如果输入换行,如何继续scanf?

时间:2013-01-28 22:37:13

标签: c

  

可能重复:
  exiting a while loop with a negative integer

我希望用户输入几个不同的数字,然后分别测试每个数字。

此外,当输入的号码为0时,我需要终止 ONLY 程序。

这是我的代码的开头:

int userInput;
printf("please enter some numbers: \n");
while ((scanf("%d", &userInput)) == 1)
{
    ...
}

在输入0之前,如何继续获取用户的输入?

2 个答案:

答案 0 :(得分:5)

while ((scanf("%d", &userInput) == 1) && (userInput != 0))
{
   ...
}

答案 1 :(得分:4)

while (scanf("%d", &userInput))
{
    if(userInput == 0)
        break;
    /* do sth */
}
相关问题