scanf()表达式被跳过
问题描述:
我的问题是第二个scanf(" %s", name);
不起作用。它只是不等待用户的输入。scanf()表达式被跳过
int main()
{
int ID = 0;
char name[100];
float CGPA = 0;
printf("enter name\n");
scanf(" %s", name);
printf("enter float\n");
scanf("%.2f", &ID);
printf("enter name\n");
scanf(" %s", name);
system("PAUSE");
}
它为什么会跳过此scanf
?
答
你的问题出在下面一行:
scanf("%.2f", &ID);
- 你有一个错误,你的意思是
&CGPA
。 - 您不需要在此指定精度(
.2
),取决于用户。简单的%f
将工作正常。
它应该是这样的:
scanf("%f", &CPGA);
希望这有助于。
+0
感谢一群人 – user1193041 2012-02-07 00:43:00
一致性很好 – sidyll 2012-02-06 21:00:45
*什么* for循环? – 2012-02-06 21:00:47
可能重复的[当我尝试扫描多个字符串在C编程有一些错误](http://stackoverflow.com/questions/9165873/when-i-try-to-scan-more-than-1 -string-in-c-programming-there-is-something-wrong) – 2012-02-06 21:11:09