2016-12-01 72 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++; 
}