how to print error msg for non numeric input

Discussion in 'Mac Programming' started by sg001, Aug 9, 2012.

  1. sg001, Aug 9, 2012
    Last edited by a moderator: Aug 12, 2012

    macrumors newbie

    Joined:
    Jul 30, 2012
    #1
    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;
       }
    ??
     
  2. sg001, Aug 10, 2012
    Last edited by a moderator: Aug 12, 2012

    thread starter macrumors newbie

    Joined:
    Jul 30, 2012
    #2
    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?
     
  3. pilotError, Aug 12, 2012
    Last edited by a moderator: Aug 12, 2012

    macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    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...
     

Share This Page