When using scanf, how do you check that only the Enter-key was pressed while an int was expected? This must be a very stupid question; sorry, I couldn't find the answer.
When using scanf, how do you check that only the Enter-key was pressed while an int was expected? This must be a very stupid question; sorry, I couldn't find the answer.
scanf is unlikely to be a good tool for the job here. I suggest:
- Use fgets to read a line. If the resulting string only consists of "\n", then only the enter key was pressed.
- Otherwise, use sscanf to parse the string for an int. (strtol is another option.)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The problem is that your scanf() call will not return until it encounters a string that is not whitespace only. You should use fgets() instead, as it prevents overflow as well.reference:Code:char input[10]; fgets(input, sizeof(input), stdin); if( input[0] == '\n' ) { puts("line is empty"); };
string - How to get empty input or only ENTER in C - Stack Overflow
"without goto we would be wtf'd"
The answer IS dead simple, and provided by @laserlight.
Don't use scanf() or getchar(). Use fgets() to get the whole string in, including the newline, and check the first char in the char array, if it is a newline.
scanf() can leave the newline in the input buffer, and getchar() does, if the user enters any chars before pressing the Enter key. Further data entry in the program is complicated by the newline left in the input buffer.
C is line buffered, and all character data entry is best handled by fgets(), The input string can then be easily parsed as needed in memory.
Please keep in mind that we speak from experience!
[QUOTE=rstanley;1289634]The answer IS dead simple, and provided by @laserlight.
Don't use scanf() or getchar(). Use fgets() to get the whole string in, including the newline, and check the first char in the char array, if it is a newline.
~~~~~~~~~~~~~~~~~~~~~~
The issue is being complicated because of needing to input an integer and not a string.
[QUOTE=zach;1289637]Using fgets(), all input from the keyboard in text. You get the string in with fgets(), and convert the string to an integer with atol(), or strtol(), confirm the value converted is within the valid range of an int, and assign to the int variable.
You could convert directly to an int, but I recommend the extra work to prevent overflow of an int, especially with atol(), which does no error checking.
This is standard for real life applications.
The user inputs an integer (or something else) as text. You then parse this into an int (or other integer type). So, it isn't complicated: you merely separate reading from parsing for better control.The issue is being complicated because of needing to input an integer and not a string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I already responded but have another question. In a number of instances in the program I use a switch statement. In those instances there are no more problems. However one situation remains. There I tried the "strtol" option you suggested. The strtol-statement generates a number okay, but the string part being output by the strtol-statement is an empty string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)