Hi,
Can someone tell me why this (otherwise working) program isnt continuing all by its own, but only when I hit a key and Enter?
Thanks
Hi,
Can someone tell me why this (otherwise working) program isnt continuing all by its own, but only when I hit a key and Enter?
Thanks
In the following line:
Because you included the newline character in scanf() you must enter that character complete the function.Code:scanf("%d\n", &n);
Jim
Yep. From scanf documentation: scanf - C++ Reference
Scanf won't start trying to match the pattern until the first time you hit enter. "%d\n" will match the number and the newline, but then due to the above quote, it'll keep matching whitespace until you put a non whitespace character in (another key and enter).Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
Very irritating, I hate scanf.
If you just use %d, a newline will be left waiting on stdin and will mess up subsequent user inputs. You can getchar() it after the scanf, or you could read a char in scanf and throw it away (see * in the link).
Thanks a lot to both of you Jim and smokeyangel!