-
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
:confused:
-
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
-
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!
-
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 :rolleyes: ). 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
:)