-1

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;
}
drez
  • 5
  • 3

2 Answers2

1

Change to:

scanf_s("%d",&ID[i]);
if (ID[i] == Sentinel_Numb)
    break;
scanf_s("%f",&seconds[i]);
if (seconds[i] == Sentinel_Numb)
    break;
stark
  • 12,615
  • 3
  • 33
  • 50
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++;
}
MD XF
  • 7,860
  • 7
  • 40
  • 71