Home > Software engineering >  do block properly break out infinite loop
do block properly break out infinite loop

Time:08-31

this(https://stackoverflow.com/a/56662680/15603477 ) answer is very good. while trying to replicate it. I found that if you type some wrong input(other than 1,2,3,4), it will be an infinite loop. I guess that mean in default part, it cannot break. How to properly refactor it to make it not infinite loop when you type the wrong input.

do {
    printf("\n\t***CHOOSE HOW TO SORT***\n\n\tBy firstname: 1\n\tBy lastname: 2\n\tBy age: 3\n\tExit Program: 4\n\n");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
        printf("\t***SORTING BY FIRSTNAME...***\n\n");
        qsort(P, n, sizeof *P, Person_cmp_firstname);
        printf("\t***DONE***\n\n");
        for (i = 0; i < n; i  ) {
            printf("Firstname: %s\t| Lastname: %s\t| Age: %d\n", P[i]->firstname, P[i]->lastname, P[i]->age);
        }
        break;
        
        case 2:
        printf("\t***SORTING BY LASTNAME...***\n\n");
        qsort(P, n, sizeof *P, Person_cmp_lastname);
        printf("\t***DONE***\n\n");
        for (i = 0; i < n; i  ) {
            printf("Firstname: %s\t| Lastname: %s\t| Age: %d\n", P[i]->firstname, P[i]->lastname, P[i]->age);
        }
        break;
        
        case 3:
        printf("\t***SORTING BY AGE...***\n\n");
        qsort(P, n, sizeof *P, Person_cmp_age);
        printf("\t***DONE***\n\n");
        for (i = 0; i < n; i  ) {
            printf("Firstname: %s\t| Lastname: %s\t| Age: %d\n",P[i]->firstname, P[i]->lastname, P[i]->age);
        }
        break;
        
        case 4:
        printf("\t***EXITING PROGRAM***\n\n");
        for (i = 0; i < n; i  ) {
            free(P[i]);
        }
        exit(0);
        
        default:
        printf("\t***INVALID OPTION***\n\n");
        break;
    }
} while (1);

update:

So far what I come from. add new variable count, begin with 0. then

...
default:
            printf("\t***INVALID OPTION***\n\n");
            count = count   2;
            break;
        }
    }while(count < 2);

CodePudding user response:

There are actually situations, where the goto label has a valid use. Usually goto is hated. But it is exactly what you need.

int main() {
    do {
        ...
        switch (choice) {
        ...
        default:
            printf("\t***INVALID OPTION***\n\n");
            goto invalid_input_found;
        }
    } while (1);
invalid_input_found:
    handle_error();
}

Your idea with a variable is great too, but an integer that you increment by 2 is a bit confusion. Maybe use a boolean value from stdbool.h for readability.

#include<stdbool.h>

int main() {
    bool run_loop = true;
    do {
        ...
        switch (choice) {
        ...
        default:
            printf("\t***INVALID OPTION***\n\n");
            run_loop = false;
            break;
        }
    } while (run_loop);
    handle_error();
}
  •  Tags:  
  • c
  • Related