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:
Thanks for any help on this.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; }
DD
![]()



LinkBack URL
About LinkBacks





). Well, I knew I still have so much more to learn, this just proves it.