I have C code doing some calculations (irrelevant to my question, I believe ). The program will ask for some parameters for calculation. The problem is when I running the codes, a scanf("%c", &ch) isn't working properly.
我有C代码做一些计算(我认为与我的问题无关)。程序将要求一些参数进行计算。问题是当我运行代码时,一个scanf(“%c”,&ch)不能正常工作。
I'm interested in whether you can reproduce this problem because it seems that I didn't get anything wrong, am I?
我感兴趣的是你是否可以重现这个问题因为看起来我没有做错什么,是吗?
I posted an compilable and shortened version of my program.
我发布了我的程序的一个可编译和缩短版本。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
float Dia_MM, Dia_I, Se1, Se, Sut = 75.00;
float Ka, Kb, Kc, Kd, Ke, Kf;
char Ch;
char Bt;
float Reli;
printf("Please input the surface condition of the shaft: G, M, H or A\n");
scanf("%c", &Ch);
// getchar();
printf("Please input the diameter of the shaft in inch\n");
scanf("%f", &Dia_I);
printf("Please specify whether your shaft is in bending (B) or torsion (T)");
scanf("%c", &Bt);// THIS LINE IS JUST SKIPPED
exit(0);
}
The GDB log is listed:
GDB日志列示:
Breakpoint 1, main () at main.c:25
25 float Dia_MM, Dia_I, Se1, Se, Sut = 75.00;
(gdb) n
30 printf("Please input the surface condition of the shaft: G, M, H or A\n");
(gdb) n
Please input the surface condition of the shaft: G, M, H or A
31 scanf("%c", &Ch);
(gdb) G
Undefined command: "G". Try "help".
(gdb) n
G
33 printf("Please input the diameter of the shaft in inch\n");
(gdb) n
Please input the diameter of the shaft in inch
34 scanf("%f", &Dia_I);
(gdb) n
4.5
35 printf("Please specify whether your shaft is in bending (B) or torsion (T)");
(gdb) n
36 scanf("%c", &Bt);
(gdb) n //PROBLEM HERE. SCANF() GOES BEFORE TAKE ANY INPUT.
37 exit(0);
3 个解决方案
#1
4
scanf()
does not consume trailing newlines. The skipped scanf()
receives the newline from the previous line typed by the user and terminates without receiving more input as you would expect...
scanf()不消耗尾线。跳过的scanf()从用户输入的前一行接收换行,并在不接收更多输入的情况下终止。
scanf()
is a bit cumbersome with newlines. A possible solution would be to use fgets()
to get a line from the console and then employ sscanf()
to parse the received string.
scanf()对于换行来说有点麻烦。一种可能的解决方案是使用fgets()从控制台获取一行,然后使用sscanf()解析接收到的字符串。
Another, more targeted, solution would be to use " %c"
in the format string of the last scanf()
call. The %c
format specifier does not consume leading whitespace on its own, which is why it gets the remaining newline, rather than a character typed by the user.
另一个更有针对性的解决方案是在最后一次scanf()调用的格式字符串中使用“%c”。%c格式说明符本身不使用前导空白,这就是为什么它获得剩余的换行符,而不是用户键入的字符。
#2
4
As thkala told above scanf()
does not consume trailing newlines.But there is another way to absorb the newline from the previous line using \n
like that scanf("%c\n",...)
.
正如thkala在上面所说的,scanf()不消耗尾线。但是有另一种方法可以利用类似于scanf(“%c\n”,…)的\n从上一行中吸收换行。
#3
0
You can also use
您还可以使用
scanf(" %c",&c);
#1
4
scanf()
does not consume trailing newlines. The skipped scanf()
receives the newline from the previous line typed by the user and terminates without receiving more input as you would expect...
scanf()不消耗尾线。跳过的scanf()从用户输入的前一行接收换行,并在不接收更多输入的情况下终止。
scanf()
is a bit cumbersome with newlines. A possible solution would be to use fgets()
to get a line from the console and then employ sscanf()
to parse the received string.
scanf()对于换行来说有点麻烦。一种可能的解决方案是使用fgets()从控制台获取一行,然后使用sscanf()解析接收到的字符串。
Another, more targeted, solution would be to use " %c"
in the format string of the last scanf()
call. The %c
format specifier does not consume leading whitespace on its own, which is why it gets the remaining newline, rather than a character typed by the user.
另一个更有针对性的解决方案是在最后一次scanf()调用的格式字符串中使用“%c”。%c格式说明符本身不使用前导空白,这就是为什么它获得剩余的换行符,而不是用户键入的字符。
#2
4
As thkala told above scanf()
does not consume trailing newlines.But there is another way to absorb the newline from the previous line using \n
like that scanf("%c\n",...)
.
正如thkala在上面所说的,scanf()不消耗尾线。但是有另一种方法可以利用类似于scanf(“%c\n”,…)的\n从上一行中吸收换行。
#3
0
You can also use
您还可以使用
scanf(" %c",&c);