Jono
Jono

Reputation: 223

C: Scanf Validation

Say I have

[number of rooms]

[room name] [num of item1] [num of item2]

...

[number of doors]

[room name] [L|R|U|D] [room name]

...

[start room] [exit room]

[move1]

[move2]

....

say I am using scanf,

scanf(" %d", &roomCount);

scanf(" %s %d %d", room_name, &item1No, &item2No);

etc...

How would I verify that the user input the correct input?

Thanks

Upvotes: 0

Views: 2219

Answers (4)

John Bode
John Bode

Reputation: 123468

scanf isn't a good tool for interactive input.

Better to read your whole input line as text using fgets, then break it into tokens using strtok and convert each value using strtol or strtoul. This will give you more control, and you don't have to worry about mistakenly accepting some bad input (for example, if someone enters "10p", scanf will successfully convert and assign the 10 and leave p in the input stream to foul up the next read; you'd probably want to reject the entire input outright).

Upvotes: 0

abeln
abeln

Reputation: 3759

Well, scanf does return the number of items read successfully. You can compare that value after every read to the number of items you were expecting.

A very simple example

#include <stdio.h>

int main() {
    int v;
    if ( scanf( "%d", &v ) != 1 ) {
      /* we know something went wrong */
    }    
}

Upvotes: 3

Mahesh
Mahesh

Reputation: 34625

int input ;
if (scanf("%d", (&input)) == 0)
{
    while (getchar() != '\n');    // Removes the offending characters that causes 
                                    // scanf to struck at error occured on future 
                                    // operations.
    printf(" \n Invalid value.\n") ;
}

So, if you wish to user to prompt again for input, keep it in a while and break it if the scanf returned value is not 0. Hope this gives you an idea.

Upvotes: 2

Gareth McCaughan
Gareth McCaughan

Reputation: 19981

Using scanf is almost always a bad idea: it doesn't distinguish between newlines and other whitespace, which makes it very easy not to know where you are in whatever thing you're trying to parse. Probably bettter: read in lines using fgets and feed each line to sscanf, checking the return value to make sure you got what you were looking for. Alternatively, use an actual parser: make your own using a parser generator like flex, or use some sort of XML for your data format and use an XML parser, or something.

It would probably be wise to design your data format so that it's harder for parsing to get out of sync without noticing, and to make the structure of the data more apparent to human readers.

Upvotes: 4

Related Questions