scanf() error checking
Currently if this code is run and the user inputs the number 1 or 2 followed by any amount of characters it will take the value 1 or 2 and run. I would like ths case (Number,Char) to be returned as an error. How might this be achieved?
This code is asking the user for a menu selection of either 1 or 2, scaning it into the int menu_choice and then checking it for errors.
printf("Please Enter Your Choice From The Menu: ");
error = scanf("%d",&menu_choice);
} while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
I know fflush(stdin); is not the correct way to flush the input but i am willing to run with it.
Thanks in advance!
>I would like ths case (Number,Char) to be returned as an error.
It's usually better to handle such things gracefully. Returning an error is tantamount to saying "I can't fulfill my promise because something is stopping me". If you have the correct menu option, the function can fulfill its promise, so you have an unusual condition that should be handled, not an error.
However, if you really want to return an error, grab the next character after your number. If scanf returns a real error or the number is not valid you can die with an exception, otherwise everything is still kosher so you check the next character from the stream. If that character is a newline then everything is just fine. Otherwise there is extraneous data, which matches your error condition:
>I know fflush(stdin); is not the correct way to flush the input but i am willing to run with it.
int main ( void )
if ( scanf ( "%d%c", &i, &c ) != 2 )
printf ( "Some other error\n" );
else if ( i != 1 && i != 2 )
printf ( "Invalid option\n" );
else if ( c != '\n' )
printf ( "Extraneous input\n" );
printf ( "Good\n" );
That's your problem then. Just be sure to comment your use of it when posting code here so that we don't waste our time telling you something you already know but choose to ignore.
Prelude, it looks like you wasted your time two years ago as well.
He doesn't want to learn, and seems to enjoy falling into the same old traps over and over.
So for one, I'll leave him be from now on.
>Prelude, it looks like you wasted your time two years ago as well.
Wow, what an old thread. When I first started coming here I lacked fundamental knowledge despite working professionally. Scary, isn't it? ;)
>So for one, I'll leave him be from now on.
That is an old post! I was doing a course on Computer Sicence 2 years ago and i droped out and took a year off. I just checked to see if my old login name was still working and it was!
Well Im working your sugestion into my program at the moment and it was a great help.
And i hope ur not serious about ignoring me cos i forgot all the stuff i had learned b4.
>And i hope ur not serious about ignoring me
Not really. I only ignore real idiots. You haven't given me any reason to think that you're a real idiot, so you're safe. :D
This is what im going to run with now i think. Thanks For all the help guys.
printf("Please Enter Your Choice From The Menu: \t");
error = scanf("%d%c",&menu_choice, &c);
fflush(stdin); /* I know it's not correct */
} while ( error != 2 || menu_choice < 1 || menu_choice > 2 || c != '\n' );
You could always just consult the FAQ with regards to reading numbers effectively. Input buffer flushing is in there as well, if you'd only look for it. ;)