c - 如果输入换行符,如何继续扫描?

标签 c

<分区>

Possible Duplicate:
exiting a while loop with a negative integer

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

此外,当输入数字 0 时,我还需要 终止程序。

这是我的代码的开头:

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

如何在输入 0 之前一直从用户那里获取输入?

最佳答案

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

关于c - 如果输入换行符,如何继续扫描?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572534/

相关文章:

c - C中输入循环的范围

c - "goto"C 中的行为 : how symbols are handled?

c - 从 double 到 float 的转换是否总是潜在地调用未定义的行为?

c++ - 使用 openmp 进行 LU 分解

c - 错误 :Assignment makes pointer from integer without a cast

sbrk(0) 会失败吗?

python - 使用 ctypes 使用 Ctrl-C 中断 Python C 扩展

python - SWIG 结构指针作为输出参数

c - _mm_storeu_si128 和 _mm_loadu_si128 之间的区别

c - scanf 之后 fgets 不起作用