you should break out of the loop after the user has done what you asked for
Printable View
I don't even get a chance to enter more input. When i enter the incorrect information and press enter the text just starts scrolling across the screen. I have no opportunity to try entering the correct number a second time around.
Again you have to flush the buffer, especially when using Visual C++. This is somewhat liken to having to call and extra getchar() after any user input.
I'm not using Visual C++. I am writing in C. I am just using this complier to write the program in. Could you please just tell me how to flush it? I tried and it didn't work. I have googled it and I can't find the information.
Okay, I got it to work. I broke the while loop by adding flushall();
Thank you all for your help!
That is the wrong solution - flushall is a function that isn't guaranteed to do anything useful or meaningfull for input channels. _DO_ look up the FAQ link that was posted a few posts back. It contains a loop that does getchar(), which is safe and correct.
--
Mats
I did look at the FAQs, but I didn't understand how to implement the flush. Would someone please just paste it into my code and help me with this?
put int ch in with your variables, and the line starting with "while" where you want to clean up your input buffer. Calling this after any scanf should be fine (assuming you don't expect someone to enter a bunch of stuff on one line, but you read it using several scanf stataments, e.g a loop to read 10 integers, and someone expects to be able to enter the 10 numbers in a line).Code:int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
You could also put all of the above code into a function, and then call that function when you feel a need to clean up the input.
The above function is ALSO very useful if you are mixing scanf() and fgets() or scanf with "%s" or "%c", since scanf leaves newlines in the input buffer, and when you then call fgets, it sees the newline from your the input to scanf, and takes the input to be blank.
--
Mats