2012-02-06 58 views
-4

我的问题是第二个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

+0

一致性很好 – sidyll 2012-02-06 21:00:45

+0

*什么* for循环? – 2012-02-06 21:00:47

+0

可能重复的[当我尝试扫描多个字符串在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

回答

3

你的问题出在下面一行:

scanf("%.2f", &ID); 
  1. 你有一个错误,你的意思是&CGPA
  2. 您不需要在此指定精度(.2),取决于用户。简单的%f将工作正常。

它应该是这样的:

scanf("%f", &CPGA); 

希望这有助于。

+0

感谢一群人 – user1193041 2012-02-07 00:43:00