ggngn
ggngn
Last edited by transgalactic2; 01-22-2009 at 04: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]
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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 02: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.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler