dimanche 24 octobre 2021

In C programming - How to get new scanf input after this if statement breaks the loop

This is the first part of my code. An array of 5 numbers are entered. Then it writes them out and accumulates them.

I want to reenter the loop with the goto TAG if a number is not entered. Problem is when the for loop is interrupted by a non numerical value and writes "That is not a number" im not asked to input another value - it just goes into an endless loop.

How do i fix this?


int main(){
    double i;

    const int ARRAYSIZE = 5;

    double array1[ARRAYSIZE];
    double array2[ARRAYSIZE];

    TAG:
     printf("Input a total of %d. numbers.\n", ARRAYSIZE);
     printf("The numbers will be accumulated:\n");
     fflush(stdout);


     for(int i = 0; i < ARRAYSIZE;  i++){
         if(scanf("%lf", &array1[i]) != 1)

         {
             printf("That is not a number - try again\n");
             goto TAG;
            
         }
     }

Aucun commentaire:

Enregistrer un commentaire