2014-10-16 78 views
2

我有問題掃描char變量,我的代碼是無法掃描掃描INT後char變量用C

#include <stdio.h> 
#include <conio.h> 

void main() 
{ 
clrscr(); 
int a; 
float b; 
char c; 

printf("Enter value for int variable \n"); 
scanf("%d",&a); 

printf("Enter value for float variable \n"); 
scanf("%f",&b); 

printf("Enter value for char variable \n"); 
scanf("%c",&c); //scanning is automatically skipped !!! 

getch(); 
} 

請你告訴我,爲什麼會出現這種情況,我能做些什麼來解決吧!

+1

尾隨新行,在SO – P0W 2014-10-16 15:10:01

+0

上有幾個副本我會在傳遞給'scanf'的每個字符串末尾添加''\ n''。 – 2014-10-16 15:14:16

回答

0

因爲存儲輸入按鍵[視爲字符輸入]。在第三個scanf()之前使用一個getch();

或者,使用(scanf(" %c",&c);) [請注意%c之前的空格],它將在實際輸入之前擺脫任何數量的空白[緩衝]字符。