I was merely repeating what was quoted. But anyway, as mentioned, the code doesn't really handle EOF.
How did you learn about that in the first place?
So I replaced fflush(stdin). Is this better?
Code:#include <stdio.h> int main() { int number=10, ch; const char *numbers[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; printf("Input a number [0-9]: "); while ( !scanf("%d", &number) || !(number >= 0 && number <=9) ) { printf("Sorry input outside of valid range: Try again Please\n"); printf("Input a number [0-9]: "); //replaced fflush(stdin); while ((ch = getchar()) != '\n' && ch != EOF); } printf("You entered the number %s\n", numbers[number] ); return 0; }
What situation?
Actually, a member on the board once quoted that for the first time long ago. I copied it into my signature and people simply keep quoting it (mostly) instead of quoting something else. Many other members on the board have been graciously thanked, as well.Perhaps, I should just skip that and feed your ego ;-)
The situation where fflush doesn't work as excepted. This is my first time using it and it seemed to work. What situation caused you to notice that it returned unpred results? and learn this secondary tech?
I have never used fflush(stdin), and until it was pointed out to me that it actually works this way IN SOME compilers (or some C runtime libraries, to be more precise), I had no idea that there was some valid work being done by fflush(stdin) - the reason being that it's very clear in the C standard what fflush does - it flushes the OUTPUT of the buffer to the actual file or device that it's representing. This is obviously meaningless on input files, and it's a specific extension added by some compiler vendors. Other compiler vendors are not doing this, so it won't work. It's also completely possible that fflush() on an input file actually causes a crash or other unwanted behaviour.
Edit: So there's no EXPECTATION that fflush() should work on input files - only output files.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I see. So in reality it means about as much to stdin as rewind would. thanks.
Ctrl + Z... Though EOF varies... But its always been Ctrl + Z on windows.
The thing is that some things work on some compilers and some not. Such as void main. On Visual Studio, the return will always be 0 if you use void main. However, on GCC it returns garbage.
We like to call it undefined behaviour because the standard does not say there is a void main. The standard can also use a "implementation dependant" term which means the implementers are free to do it like they want.
Like std::string is not required to put a null char at the end of the string - it's implementation dependant, so you shouldn't rely on it.
The same goes for fflush(stdin). The standard does not mention it and its behaviour is therefore not defined, technically then, undefined.