c中的哨兵循环执行
sentinel loop execution in c
我有以下代码,我希望它在只输入一次哨兵号码后中断,但目前我必须输入两次哨兵号码才能中断代码。有什么想法吗?
#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;
while (1)
{
scanf_s("%d",&ID[i]);
if (ID[i] == Sentinel_Numb)
break;
scanf_s("%f", &seconds[i]);
if (seconds[i] == Sentinel_Numb)
break;
i++;
}
我有以下代码,我希望它在只输入一次哨兵号码后中断,但目前我必须输入两次哨兵号码才能中断代码。有什么想法吗?
#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;
while (1)
{
scanf_s("%d",&ID[i]);
if (ID[i] == Sentinel_Numb)
break;
scanf_s("%f", &seconds[i]);
if (seconds[i] == Sentinel_Numb)
break;
i++;
}