drez drez - 1 day ago 4
C Question

sentinel loop execution in c

I have the following code and I would like it to break after entering the sentinel number only once but at the moment I have to input the sentinel number twice for the code to break. Any ideas?

#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;
}

Answer

Change to:

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