Thread: Correct use of isdigit

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Correct use of isdigit

    I am trying to check to ensure the user entered a number instead of a character for this program. I was thinking of using isalpha and saying if that isalpha is true then tell user and end. But I have reconsidered and think that what I really need to do is use isdigit, and say that if isdigit is false (i.e the user inputs anything other then a number), then tell the user and exit.

    But I am still stuck with isdigit like I was isalpha. I have tried various combinations on trying to get it to recognize the if statement and have had no luck. Currently, if I put in a character instead of a digit, it ignores my isdigit statement and thinks it has all the data it needs to go to the end instead of exiting. Can someone please sheed some light onto what I am doing wrong here. Or perhaps should I be trying something else entirely for what I am trying to do?

    My current code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main (void)
    {
    typedef struct
    	{
    	int hr;
    	int min;
    	int sec;
    	} TIME;
    
    TIME sTime;
    TIME fTime;
    TIME tTime;
    
    printf("Calculate total time between start and finish. Format for data is hh:mm:ss\n");
    
    printf("\nEnter a start time:\t");
    scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    
    if (sTime.hr >24 || sTime.min >60 || sTime.sec >60)
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to exit.\n");
    		getch();
    		return 0;
     		}
    
    if (isdigit(sTime.hr || sTime.min || sTime.sec))
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to exit.\n");
    		getch();
    		return 0;
     		}
    
    printf("\nEnter a finish time:\t");
    scanf("%d:%d:%d", &fTime.hr, &fTime.min, &fTime.sec);
    
    if (fTime.hr >24 || fTime.min >60 || fTime.sec >60)
    		{
    		printf("\aInvalid Option\n");
    		printf("\nPress any key to exit.\n");
    		getch();
    		return 0;
     		}
    
    tTime.hr = fTime.hr - sTime.hr;
    tTime.min = fTime.min - sTime.min;
    tTime.sec = fTime.sec - sTime.sec;
    if (tTime.hr < 0)
    	tTime.hr += 12;
    
    printf("\n\n\nThe total time:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
    
    return 0;
    }
    Thanks for any help on this.
    DD
    "aut vincere aut mori"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    By the time you get to that part of the code, you're too late

    To make sure the user entered 3 numbers, do this
    Code:
    if ( scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec) != 3 ) {
      printf("Incorrect format - try again\n" );
      while ( getchar() != '\n' ); /* throw away junk user input */
    }
    Basically, this code loops until the user enters 3 integers

    Once that is done, then you can perform your range tests like
    if (sTime.hr >24
    etc

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're going about things the wrong way.

    This:
    >scanf("%d:%d:%d", &sTime.hr, &sTime.min, &sTime.sec);
    will ensure that only numbers are read into the variables. If the users enters a character by mistake, scanf() will stop in it's tracks and not populate the remaining variables. You *must* check it's return code to ensure that it has done what you asked. In your examle, the return code should be 3, the number of variables populated with data from the scan.

    If you input using scanf(), you don't need to use isdigit().

    >if (isdigit(sTime.hr || sTime.min || sTime.sec))
    Assuming you change your code to input a string, you'd need to use isdigit(). This function only takes one arg, which is an int (representing a char). You would normally call it like this:
    >isdigit(MyVar)
    You cannot do it in the syntax you have used.

    [edit] Beaten!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Arrow OK

    Well that explains why it was not working...If it wont work that way, then I will never get it to work, will I.

    Thanks guys. To me it seemed like such a simple way to check the input (of course on a good day I like to think that fast food really wont kill me either ). Well, I knew I still have so much more to learn, this just proves it.

    So basically I needed to check for illegal input while there were inputing it, and if I wanted to use isdigit or isalpha I need to be doing input as a string, and only checking one variable at a time. Kind of makes sense, I think.

    Thanks again
    DD
    "aut vincere aut mori"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. isdigit issue - help
    By vajrapani in forum C Programming
    Replies: 3
    Last Post: 08-13-2008, 02:16 AM
  4. correct malloc use
    By s_siouris in forum C Programming
    Replies: 10
    Last Post: 05-28-2008, 10:49 PM
  5. isdigit confusion
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 04-27-2002, 04:55 AM