Thread: invalid input

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    invalid input

    I need my program to output a message "invalid input" when the input is not correct by the user.

    Currently my programs use is to allow the user to enter a number and the program outputs that number in words.

    This is whats meant to happen:

    Input: 30
    Output: thirty
    Input: 30 eight
    Output: invalid input

    This is what happens:

    Input: 30
    Output: thirty
    Input 30 eight
    Output: thirty

    Can anyone help me with how to get the invalid input thing to work. Here is a snippet of my code:

    Code:
    int res;
          int num;
          res = sscanf( cmd, "%d", &num);
          switch(num){
          
          /*0 - 20*/
          
             case 0: printf("zero\n");
                break;
             case 1: printf("one\n");
                break;
             case 2: printf("two\n");
                break;
             case 3: printf("three\n");
                break;
             case 4: printf("four\n");
                break;

  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
    > res = sscanf( cmd, "%d", &num);
    This only tells you that there was a valid number at the start of the line.

    res = sscanf( cmd, "%d%n", &num, &pos);
    if ( cmd[pos] != '\n' ) // there's trailing chars, probably its an invalid line
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    But theres a possibility that theres trailing chars, coz the numbers go up to 1000. I basically need to say that if a number inst entered its invalid input dont i? how do i go about doing that?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    It wouldnt catch out all the invalid input by saying if theres any more chars after [3] its invalid input. How do i get it to catch it out if say the user entered: 56hj for example?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm guessing my previous example would result in cmd[pos] == 'h' in that case....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    This catches the invalid input but wont print out the number now when it is valid input.

    Code:
          int res;
          int num;
          int pos;
         /* res = sscanf( cmd, "%d", &num); */
          res = sscanf( cmd, "%d%n", &num, &pos);
          if ( cmd[pos] != '\n' )
             switch(num){
             
             /*0 - 20*/
             
                case 0: printf("zero\n");
                   break;
                case 1: printf("one\n");
                   break;
                case 2: printf("two\n");
                   break;

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    and if i enter 7hello for example. it prints out seven.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by sworc66
    and if i enter 7hello for example. it prints out seven.
    try debugging by adding a printf() statement to ouput pos as an decimal and cmd[pos] as a hex value. This will tell you if you're getting the values you expect.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This catches the invalid input but wont print out the number now when it is valid input.
    Code:
    if ( cmd[pos] != '\n' )
        switch(num){
    Well of course it's not going to work. You're telling it "if this spot IS NOT a newline, then process that as a valid number".

    That's the opposite of what you want. You want it to process it if the next position is the newline, meaning, there is no invalid data.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM