2016-12-01 104 views
-1

我有下面的代码,我希望它在进入哨兵号码后只能打破一次,但此刻我必须输入两次哨兵号码来破解代码。有任何想法吗?哨兵循环执行在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; 
} 
+0

我想'i'定义和初始化的地方? – yano

+0

不编译...“我”没有定义。 – TonyB

+0

抱歉只编辑了问题来初始化我 – drez

回答

1

更改为:

scanf_s("%d",&ID[i]); 
if (ID[i] == Sentinel_Numb) 
    break; 
scanf_s("%f",&seconds[i]); 
if (seconds[i] == Sentinel_Numb) 
    break; 
0
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++; 
}