ggngn
This is a discussion on input checking problem.. within the C Programming forums, part of the General Programming Boards category; ggngn...
ggngn
Last edited by transgalactic2; 01-22-2009 at 03:14 PM.
One suggestion is to read a whole lines worth of input into a char array variable and then use the strtod function on that buffer. If the "end" pointer argument to that function points to a null then the information was parsed properly. If it points to something else, i.e. the conversion of the text into an integer stopped short for some reason (such as encountering the first 'g' in "1gghjk"), then you know something other than a simple integer value was entered and can reprompt for more data.
Incidentally, I would not use recursion for the clean and main_menu functions. The reason you are using it in this instance is not something recursion was really meant to solve.
[edit]Oh, and main returns an int.[/edit]
I used to be an adventurer like you... then I took an arrow to the knee.
This is a little smoother, imo:
The program keeps looping back to query the user and get the input value,Code:input1 = 0; while(input1 == 0) { printf(" Enter Your Choice: "); input1 = scanf("%d", &opt); }
rather than just ask once, and then run on. Very easy to add all kinds of test
for good input, as well.
i solved it
Last edited by transgalactic2; 01-22-2009 at 01:31 PM.
Uh, no...
That still has the problem with "1gghjk", the scanf call will return 1 and it will also return 1 to the opt variable. The rest of the string "gghjk" (garbage) will still be in the input stream/buffer. And god forbid you enter something that doesn't start with a numerical char like "gghjk1", the scanf will return 0 since there was nothing to convert into an int and the stream will still have the "gghjk" in it and you'll loop endlessly.
I used to be an adventurer like you... then I took an arrow to the knee.
Surprised there isn't a flush in there. Adak's solution looks almost exactly like what I came up with on my own in my programs, except I added a flush after the scanf line.
Code:#define FLUSH while (getchar() != '\n')If this is his only input line in the program, he may not need to do the define, just put the while loop in there directly.Code:input1 = 0; while(input1 == 0) { printf(" Enter Your Choice: "); input1 = scanf("%d", &opt); FLUSH; }
All of this presupposes that he can even use loops, though. His teacher, for whatever reason that god only knows, seems hell bent on banning loops from the class. He's posted like 10 different things, all of them "how do you do this without a loop?" In which case, he'll have to figure out some other way of doing this.
what will happen with your code if scanf or getchar returns EOF?
If I have eight hours for cutting wood, I spend six sharpening my axe.
Do not edit out your posts once you get your answer! How is anyone else having the same trouble supposed to know how you solved it?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^