我有C代碼做一些計算(我認爲與我的問題無關)。該程序將要求一些參數進行計算。問題是當我運行代碼時,scanf(「%c」,& ch)工作不正常。scanf()不帶任何輸入
我對是否可以重現此問題感興趣,因爲它似乎沒有出現任何問題,是嗎?
我發佈了一個可編輯和縮短版本的程序。
#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);
}
的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);
「請指定軸是否彎曲或扭轉。」 * chortle * – x0n 2012-02-18 23:06:44
@ x0n:我會更加擔心振動:-) – thkala 2012-02-18 23:08:44