C 中不需要的双重重复
Unwanted double repetition in C
假设我正在编写这段代码:
char c; // c from choise.
do{
printf("Please choose one of the following operators:\n");
printf("+ for Addition\n- for Subtraction\n* for Multiplication\n/ for Division\n");
printf("= for Assignment and\nE to check if two variables have the same type and value.\n");
scanf("%c", &c);
}while(c!='+' && c!='-' && c!='*' && c!='/' && c!='=' && c!='E');
主要问题是,当用户输入数字或字母时,除了上述运算符之外的所有其他内容,printf 中的每条消息都会在屏幕上出现两次。
例如,如果 c='A' 我们有:
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
A //wrong answer, the loop continues...
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
//here the program awaits the new value of c.
但是,如果 c='-' 或 c='+' 等,我们当然有
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
- // or +
//the program goes to the next line.
当我尝试在 while 或 for 循环中转换 do_while 循环时发生了同样的事情。
在用户键入错误字符后,stdin 缓冲区中仍然(至少)有一个换行符。
@haccks 的评论是解决问题的一种方法。
另一种方法是通过调用 getchar 的短循环跟随 scanf,直到收到 EOF。
在%c
前加一个space:scanf(" %c", &c);
。这是因为从先前输入输入的换行符可能存储在 c
中。
您还可以在 scanf()
之后使用 getchar()
清除输入缓冲区。
假设我正在编写这段代码:
char c; // c from choise.
do{
printf("Please choose one of the following operators:\n");
printf("+ for Addition\n- for Subtraction\n* for Multiplication\n/ for Division\n");
printf("= for Assignment and\nE to check if two variables have the same type and value.\n");
scanf("%c", &c);
}while(c!='+' && c!='-' && c!='*' && c!='/' && c!='=' && c!='E');
主要问题是,当用户输入数字或字母时,除了上述运算符之外的所有其他内容,printf 中的每条消息都会在屏幕上出现两次。
例如,如果 c='A' 我们有:
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
A //wrong answer, the loop continues...
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
//here the program awaits the new value of c.
但是,如果 c='-' 或 c='+' 等,我们当然有
Please choose one of the following operators:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
= for Assignment and
E to check if two variables have the same type and value.
- // or +
//the program goes to the next line.
当我尝试在 while 或 for 循环中转换 do_while 循环时发生了同样的事情。
在用户键入错误字符后,stdin 缓冲区中仍然(至少)有一个换行符。
@haccks 的评论是解决问题的一种方法。 另一种方法是通过调用 getchar 的短循环跟随 scanf,直到收到 EOF。
在%c
前加一个space:scanf(" %c", &c);
。这是因为从先前输入输入的换行符可能存储在 c
中。
您还可以在 scanf()
之后使用 getchar()
清除输入缓冲区。