c - C中的哨兵循环执行

标签 c loops sentinel

我有以下代码,我希望它在仅输入一次哨兵号码后就中断,但目前我必须输入两次哨兵号码才能中断代码。有什么想法吗?

#include <stdio.h>// Include a standard C Library
#define Sentinel_Numb -1
int main(){

  int ID[360];              
  float seconds[360];
  int i = 0;

 printf("please enter the  ID and how many seconds to calculate enter  -1 when done\n");

while (1)                           
{
    scanf_s("%d",&ID[i]);
    scanf_s("%f",&seconds[i]);

    if( ID[i] == Sentinel_Numb || seconds[i] == Sentinel_Numb){
       break;
         }
    i++;
}

return 0;
}

最佳答案

更改为:

scanf_s("%d",&ID[i]);
if (ID[i] == Sentinel_Numb)
    break;
scanf_s("%f",&seconds[i]);
if (seconds[i] == Sentinel_Numb)
    break;

关于c - C中的哨兵循环执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922455/

相关文章:

c - 程序在 printf 上挂起或跳过 printf

java - 如何在 Java 中每次迭代时清空循环内的缓冲区?

Pandas:使用嵌套 for 循环填充空列时未获得所需的输出

python - 在 python 中执行 oct2py 以运行 Octave 脚本时发生哨兵错误

c - 如何在结构数组的末尾提供哨兵值

c - 为什么这段代码在选择排序中给出段错误,请解释

c - 用 C 符号对文件进行编码

C的隐式类型转换

java - 使用时钟 while 循环在 while 循环中读取文件

redis - 如何动态设置 HAProxy IP 配置?