2021年5月1日星期六

Is there a method to consider a sentinel number like -1 that exits the for loop for scanf?

I have the code below and would like to exit the first for loop whenever a -1 is entered into the scanf.

int main()  {      int  i, n, ScoresFromTest[SIZE] ;      printf("Number of scores: ") ;      scanf("%d", &n);      printf("Enter the numbers into the array:\n") ;      for(i=0 ; i<n ; i++)          if(scanf("%d", &ScoresFromTest[i]) == -1)          {              break;          }          else          {              scanf("%d", &ScoresFromTest[i]);          }      int size = sizeof(ScoresFromTest) / sizeof(int);            for(i = 0; i < n; i++)          printf("%d ", ScoresFromTest[i]);  }  
https://stackoverflow.com/questions/67352880/is-there-a-method-to-consider-a-sentinel-number-like-1-that-exits-the-for-loop May 02, 2021 at 12:03PM

没有评论:

发表评论