This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: loop bug using mingw3.2 and cygwin in M$ Windows


Gerald Lafreniere wrote:
Running this code you will produce the bug if you input anything other
than 1 2 3 or 4

The problem here is that you are reading from stdin which is line buffered, so getchar does not return until the user hits return. return however is a character too, so if the user typed "5<RET>" then we have two characters, and we end up looping twice.


This is easier to see if you type something like "5 6 7<RET>" in which case we loop 6 times before waiting for another character.

Try checking the user input to verify that the user only typed one character, and also add code to ignore the end-of-line character.

You could also try turning off the line buffering so getchar() returns after one character, but this is more complicated, and may be OS dependent. See setvbuf, setbuf, setbuffer.
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]