我已經嘗試了下面的代碼,在輸入字符前需要%d。這是代碼中的切換循環之後。爲什麼%d在輸入字符前需要?
#include<stdio.h>
#include<conio.h>
void sum();
void mul();
void main()
{
char ch;
int c;
clrscr();
do
{
printf("\n\n Enetr choice ");
printf("\n\n\t 1: SUM \n\n\t 2: MUL");
scanf("\n\n\t %d",&c);
switch(c)
{
case 1:
sum();
break;
case 2:
mul();
break;
default:
printf("\n\n hhhh..... ");
}
printf("\n\n Want u calcualte again");
//scanf("%d");
scanf("%c",&ch);
printf("\n ch value is %c",ch);
}while(ch=='y'|| ch=='Y');
getch();
}
void sum()
{
int s;
s=10+50;
printf(" SUM: %d",s);
}
void mul()
{
int s;
s=10*50;
printf(" SUM: %d",s);
}
在這段代碼在開關後,我試圖輸入字符,但沒有在註釋中的scanf語句是需要輸入字符。沒有那個scanf語句編譯器不需要字符輸入。所以請給我解決方案。
要吸收新行 –
我認爲這可能是閱讀的換行符。 – DaoWen
http://stackoverflow.com/questions/8464620/program-doesnt-wait-for-user-input-with-scanfc-yn –