可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am learning C and I\'m using \"getchar()\" to stop the command windows so I can see the exercises am doing but it just doesn\'t work. heres a sample:
#include <stdio.h>
int main()
{
int value;
printf(\"1. option 1.\\n2. option 2.\\n3. option 3.\\n4. Exit\\n\\nMake an option: \");
scanf(\"%d\", &value);
switch (value)
{
case 1:
printf(\"you selected the option 1.\");
break;
case 2:
printf(\"you selected the option 2.\");
break;
case 3:
printf(\"you selected the option 3.\");
break;
case 4:
printf(\"goodbye\");
break;
default:
printf(\"thats not an option\");
break;
}
getchar();
return 0;
}
this is the output:
- option 1.
- option 2.
- option 3.
- Exit.
Make an option: 1
you selected the option 1.
Process returned 0 (0x0) execution time : 3.453 s
Press any key to continue.
Why doesn\'t it wait for the input of \"getchar()\"?
回答1:
Your scanf only ate the number but not the trailing newline. Putting a newline or white space after the %d will then give you the opposite problem, reading too far.
This is why people don\'t like scanf.
I would suggest reading an actual line (use fgets(3)
) and then using sscanf()
to scan the string.
回答2:
First of all, do not use fflush() to clear an input stream; the behavior is undefined:
7.19.5.2.2 If stream points to an output stream or an update stream in which the most recent
operation was not input, the fflush function causes any unwritten data for that stream
to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.
The problem is that the trailing newline is not being consumed by the \"%d\" conversion specifier, so it\'s being picked up immediately by the getchar()
. There\'s no one best way to deal with this, but generally the approach is to read the whole line as text (using either fgets()
or scanf()
with a sized \"%s\" conversion specifier), which will consume the newline, then convert to the target data type using sscanf()
or strtol()
or strtod()
.
回答3:
Can getchar be getting your carriage return that you enter after the 1?
回答4:
The getchar() is reading the \\n from the keyboard in scanf - see here for more info
回答5:
you are getting a carriage return, the way i would get around it, define a char and just have it scan the carriage return,
char ch;
(before your getch() enter the following)
scanf(\"%c\",&ch);
getchar();
should work this way, not the most efficient way to do this but works for me.
回答6:
I think you input a enter after inputting \"1\". It will be accepted by the getchar()
.So you can solve the problem simply by add an additional getchar()
after the original one(just before the return 0;
).
** I tested this and it worked.
回答7:
As mentioned already scanf leaves \\n behind after reading user input.
Soultion: add getchar() straight after scanf.
That compensates scanf deficiency.
i.e.
int value;
printf(\"1. option 1.\\n2. option 2.\\n3. option 3.\\n4. Exit\\n\\nMake an option: \");
scanf(\"%d\", &value);
getchar();
switch (value)
回答8:
while reading other answers it just came to me that you can use scanf(\"%d\\n\",&n);
instead of scanf(\"%d\",&n);
to remove new line from buffer. I haven\'t checked this out though.
Hope it works :D
回答9:
#include <stdio.h>
#include<conio.h>
void main()
{
int value;
printf(\"1. option 1.\\n2. option 2.\\n3. option 3.\\n4. Exit\\n\\nMake an option: \");
scanf(\"%d\", &value);
switch (value)
{
case 1:
printf(\"you selected the option 1.\");
break;
case 2:
printf(\"you selected the option 2.\");
break;
case 3:
printf(\"you selected the option 3.\");
break;
case 4:
printf(\"goodbye\");
break;
default:
printf(\"thats not an option\");
break;
}
getch();
}
回答10:
in order for your program to work, you should \"flush\" your input stream before calling getchar() with a call to fflush(stdin). What this does is, when you type in you keyboard a number and then the return key, the input buffer gets both characters, for example \'1\' and \'\\n\', and your call to scanf gets only \'1\' so the \'\\n\' remains in the input buffer. When you call getchar, you are \"gettin\" that remaining \'\\n\'. Flushing the input discards all the buffer.