我的代碼如下,爲什麼scanf()函數不會從用戶那裏獲得輸入?
typedef struct
{
char name[15];
char country[10];
}place_t;
int main()
{
int d;
char c;
place_t place;
printf("\nEnter the place name : ");
scanf("%s",place.name);
printf("\nEnter the coutry name : ");
scanf("%s",place.country);
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%c",&c);
printf("You entered %c",c);
return 0;
}
如果我運行程序,它會提示地名和國家名稱。但它永遠不會等待用戶輸入的字符,只是在輸出屏幕上輸出空白值。我試過
fflush(stdin);
fflush(stdout);
兩者都不會工作。
注意:如果我編寫代碼來獲取整數或浮點數,它會提示輸入值,而不是字符。
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%d",&d);
這是爲什麼發生?代碼是否有問題或者scanf是否自動從緩衝區獲取值?如何解決這個問題?