user-0 提问: 学switch体时遇到的问题

回复:
user: user-0
content: 问题总结一下： 1.在使用scanf();时，系统会把回车键放在缓冲区，如果不想在下次使用scanf时接受他，那么就使用getchar();接受掉。 2.exit正常退出的用法是 exit (0);  3.在试验中发现上述程序的16、17行，在输入时，不用输入空格，系统也会智能截断。

user: user-0
content: 问题已经解决了，那个不在scanf前使用 getchar();而不暂停屏幕的问题应该是我个人问题，今天在单位测试了一下，这个问题消失了，下班后再回去试试。

user: user-1
content: 万一你getchar()前，用户刚输入了<Tab>,<Enter>两个按键呢？

user: user-0
content: 引用 7 楼 zhao4zhong1 的回复:在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];  我昨天看到你这个回复了，只是不明白为什么不使用getchar();时，系统不是应该把10赋值给变量，然后继续执行吗， 但为什么程序在执行到getch();的时候不会暂停屏幕。

user: user-1
content: 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];

user: user-0
content: 引用 4 楼 iyomumx 的回复:exit; 不是一个函数调用，你可能要用 exit(0); 以获得预期效果 谢谢老师，还有一个问题就是为什么在第二个scanf前不加getchar(); 程序运行到这里会直接闪退， 就算把回车赋值给他，执行到getch();程序也应该暂停啊。

user: user-0
content: 引用 3 楼 weixin_30460121 的回复:因为有getch()，需要吸收一个回车  确实是这样，不过我要的是exit直接退出，是我exit用错了。

user: user-2
content: exit; 不是一个函数调用，你可能要用 exit(0); 以获得预期效果

user: user-3
content: 因为有getch()，需要吸收一个回车

user: user-1
content: Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

user: user-0
content: 好吧，问题解决了，不是我不搜索就提问，关键是不知道是什么问题。 在使用scanf之前加一个getchar();接受上次的回车就可以了。  printf("\n返回请按y,退出请按其他任意键\n"); //这里运算完毕后提示用户输入y跳转到计算入口开始新的计算     scanf("%c",&c);       if (c == 'y' || c == 'Y')      {         system("cls");         goto _calc;     }   else          exit;  还有一个问题就是为什么上列语句为什么在输入非Y退出的时候需要敲两次回车

user: user-0
content: 我的和这个是不是相同的问题 http://bbs.csdn.net/topics/391854389 把上次输入的回车键放在了缓存区，再次调用的时候 把10赋值给了c，我调试的时候c的值好像就是10。 但是就算这样为什么在scanf完加一个printf也不会输出呢,或者是输出了，不会在执行getch的时候暂停。