Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sg001

macrumors newbie
Original poster
Jul 30, 2012
9
0
How would I go about printing an error msg for non numeric inputs...

my code basically has input the time in HHMMSS

but if a user enters aabbcc i would like to print error msg

I tried this but it didn't work

Code:
if (time!=int){
       printf("Format must be HHMMSS\n");
       return 0;
   }
??
 
Last edited by a moderator:
How would I go about printing an error msg for non numeric inputs...

my code basically has input the time in HHMMSS

but if a user enters aabbcc i would like to print error msg

I tried this but it didn't work

Code:
if (time!=int){
       printf("Format must be HHMMSS\n");
       return 0;
   }
??

ok I figured it out by doing

Code:
 if(scanf("%d", &time) !=1){
    printf("Format must be HHMMSS\n");
    return 0;

but now it does not have an output when I do enter numbers for some reason?

i.e. if I enter 123543 it reads "Format must be in HHMMSS"


any suggestions on how to fix this?
 
Last edited by a moderator:
ok I figured it out by doing

Code:
if(scanf("%d", &time) !=1){
    printf("Format must be HHMMSS\n");
    return 0;

but now it does not have an output when I do enter numbers for some reason?

i.e. if I enter 123543 it reads "Format must be in HHMMSS"


any suggestions on how to fix this?

A couple of ways you can do it.

You can read it as a string and write a little function to parse/validate the string.

Code:
if (strlen(timestr) != 6) 
 printf("Invalid Time Entered");

Then break up the string into the individual components.

First two digits are the hour - copy the first two digits into another string and convert it to an integer (atoi)
Code:
if (hours < 0) || (hours > 24) 
 printf("Invalid Time, Hours needs to be 0 - 24");

Well you get the idea for the rest...
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.