Thread: IsDigit always returns 0

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    35

    IsDigit always returns 0

    I am working through some C videos and I am writing code. I have code below and I don't understand what I am doing incorrectly. ISDigit only returns a 0 no matter whether you enter a digit 0 - 9 or J or some special character so the condition always evaluates to false and the true statement is never executed.

    The code is very simple.
    It asks for a number between 1 - 255.
    (Question 1: I attempted an unsigned char for this and it would not work. Is there no such thing as an unsigned char in C or C++)

    It evaluates to input to ensure that it is a number.
    (The isdigit)

    It checks to ensure that number is between 1 - 255 otherwise it either asks the question again or just exits the program.

    If a valid entry was put in. It displays a count down starting with the number entered.

    Question 2: Can someone explain to me why this is and offer a solution that would rectify this issue?

    The code was saved as a .cpp file but the coding is actually C. I am using Dev C++ Compiler on a Windows 7 64 bit Operating System.

    Code:
    #include <conio.h>
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
                 
        int loopCounter=0,count=0,a;
        
        for(a=4;a>0;a--) {
          printf("Please enter a number 1 - 255: ");
          scanf("%d",&count);
          if(isdigit(count)) {
            if(count<=0)
              a=2;
            else if(count>0 && count<256)
              a=0;
            else
             return 1;        
          }
          else
            return 1;
        }
        while(count>=loopCounter) {
           printf("%d\n",count);
           count--;
        }
        getch();
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Use %c with scanf.

    The isdigit() function tests for a decimal digit character.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Enter numbers between 48 and 57
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you don't need the isdigit test at all. take that out. your other tests for <0 and >255 take care of the range. but if you want to check that the user didn't enter 'x' or some other invalid string, check the return code of scanf. it should return 1 indicating it was able to read 1 integer. if it returns 0, the user entered something invalid.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    How do I test the code returned from scanf?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    The same way you test the return result from isdigit()

    Namely
    Code:
    if ( scanf("%d",&count) == 1 ) {
      // do stuff
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    okay, I rewrote it to:

    Code:
    #include <conio.h>
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int loopCounter=0,count=0,a;
        
        for(a=2;a>0;a--) {
          printf("Please enter a number 1 - 255: ");
          if (scanf("%d*c",&count)) {
            if(count<=0) {
            printf("%d is not a number between 1 - 255. ",count);
            a=2;
            }
            else if(count>=256)
              a=2;
            else
             a=0;        
          }
          else {
            printf("A valid number was not entered.  The program will now close.");
            count=-1;
            a=0;
          }
        }
        while(count>=loopCounter) {
           printf("%d\n",count);
           count--;
        }
        getch();
        return EXIT_SUCCESS;
    }
    Thanks for your help everyone.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > The code was saved as a .cpp file but the coding is actually C. I am using Dev C++ Compiler on a Windows 7 64 bit Operating System.
    Erm, no it isn't.

    #include <iostream>
    using namespace std;

    This is C++.

    Do yourself a favour and start by saving your C programs in .c files.

    If you just accept "well it compiles", you're going to find yourself in all sorts of trouble later on.

    > if (scanf("%d*c",&count))
    1. What is the *c for?
    2. Have you read the manual page for scanf yet to see what return results are possible?
    Just saying if(scanf()) is true for a successful conversion, and is also true for EOF.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    I do realize that those two lines of code are C++. I was just beginning video tutorials. In the video tutorials, the guy teaches you using dev c++ and has you save your .c files as .cpp files. It automatically adds the <iostream> and the namespace. I also saved some files as .c and noticed that those were not there. I wrote the rest of my items with a .c just to be on the safe side. My next project which is also located here was saved as a .c file.

    The (supposed to be but a mistype on my part) %d%*c was because I showed the code to someone else that programs C and here is what they had to say about it. I looked in the man page and it seemed to say what the guy was telling me.

    "It seems that your call to scanf() is leaving a single character on standard input that gets grabbed by the next call to getchar() (instead of waiting for a new character), and so changing the call to
    scanf("%d%*c", &count);
    including <cstdio>, and changing the call to getchar() should work. The funny "*" notation in %*c means "convert a character, but don't try to put it in any variable because I won't provide one for it."
    It still won't be idiomatic C. Realize that it will be difficult to learn idiomatic pure C in an environment like Dev C++, because modern C++ have diverged greatly from pure C."

    The above statement from him is why on my next projects I saved it as a .c instead of a .cpp which is against what the video tutorials state to do. Video tutorials are at cplusplustutor.com. Even though the name states cplusplus, all the videos teach you C except they have you save the files as a .cpp. The guy explains this in the introduction videos.

    I have a linux vm that I opened the man page in. I didn't understand most of it. I did see this which was along the line of what the other gentleman told me:

    Each conversion specification in format begins with either the character '%' or the character sequence "%n$" (see below for the distinction) followed by:
    o An optional '*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments returned by scanf().

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > In the video tutorials, the guy teaches you using dev c++ and has you save your .c files as .cpp files.
    Which is enough to make them a steaming pile of ........ to be avoided.

    Video seems to be an awful medium for learning - megabytes of data to convey kilobytes of useful information.

    You're basically learning random bits of syntax from someone who is in the "well it compiles" camp.
    Which "works" until you move to a different compiler/environment, and then all the flaws are exposed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    I would agree if that were my sole source for learning. I am taking the Open Course Ware by Harvard for CS50 and Mobile phone programming. However in their video lectures they didn't explain items well enough for me to grasp how to accomplish something I needed to do for homework. In this particular instance, two dimensional arrays. I searched for the answers to how two dimensional arrays work and how to properly read them and I ran across tutorial after tutorial, unhelpful forum post after unhelpful form post and then I decided to search you tube for the answer and found this guy. His video tutorial made it easy for me to understand what two dimensional arrays are and how they work. For the first example assignment above I did try to do things exactly like he had them in the video however for the second assignment and assignments afterwards I will be creating everything in just .c files. He trains and explains at an extremely beginner level which I like. Given the feedback from everyone I will be trying to compile my programs on both linux and windows. Thank you for all of your insight and help.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I would agree if that were my sole source for learning.
    It doesn't matter how many sources you are using.

    If a source teaches you things that will flat out interfere with your progress you need to drop that source and find a different one.

    One final note on this, anyone can make a "YouTube" video; the site doesn't have any standards that educational material must match. The majority of videos I've seen (We've seen dozens of people brag about their new videos with links to their "channels".) are made by people barely sufficient in the language they are pretending to teach. That says nothing for the complete newbies who think they've mastered the language enough to teach it which also happens far too often.

    However in their video lectures they didn't explain items well enough for me to grasp how to accomplish something I needed to do for homework.
    Lectures are supplemental in nature. They are intended to illustrate and elaborate on material from other sources. They aren't intended to be complete in isolation.

    I don't know what Harvard does for their "OpenCourseWare". Some colleges offer supplemental class material that was written by the professors. Have you looked to see what resources beyond the video they offer?

    Soma

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    This guy states he has been programming for either 10 or 18 years in his introduction videos. They are not the best videos but they are at such a beginner level that I have to make it harder myself however I like that because he doesn't talk over my head. I don't mind them at all. I merely program everything in .c files instead of .cpp files and when he uses something that I am not quite sure about, I google it to ensure that it is correct. I don't mind it and it seems to be helping me a lot more than any of the other tutorials I have read.

    There are other courses available other than Harvard.

    MIT Open Course Ware, Computer Science --> Free Online Course Materials | Courses | MIT OpenCourseWare
    Mixed list --> Computer Science: Free Courses | Open Culture

    I have not looked at those yet. I am just aware that they exist. My plan is to listen to this guys videos so that I can continue to work through Harvards classes and then move on to other items. I seem to be understanding with this guys videos a lot better than anything else. I do like the way he explains items even though he babbles a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DialogBox returns -1. GetLastError returns error 1813.
    By Benji Wiebe in forum Windows Programming
    Replies: 14
    Last Post: 09-26-2011, 10:21 AM
  2. Operator that returns no value but returns value
    By keira in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2008, 07:22 PM
  3. main returns int -- compiler returns nonsense
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2005, 09:53 AM
  4. isdigit()
    By stargazer66 in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 07:02 PM
  5. isdigit ()
    By mackol in forum C++ Programming
    Replies: 8
    Last Post: 01-18-2003, 11:03 PM