Home > OS >  Infinite error loop with if/else statement in a while loop
Infinite error loop with if/else statement in a while loop

Time:01-01

I'm trying to make a calculator with 12 operations, the 12th being exit, and I want it to produce an error message if the user tries to input a value other than 1-12.

I got it to work for numbers like 15, 500, etc. that aren't in the range, but if the user inputs the letter 'a' for example, it results in an infinite loop, while if the user enters 500 it does what I want it to, which is print the "try again" message and display the menu again.

So, I know the problem is with the if/else loop directly contained in the while loop, but I'm not sure why it doesn't return to the menu after the break; statement in the else statement containing "red" (I put red and blue so that I could tell which statement is being printed). I tried a do/while loop but had the same issue. I also tried making the default statement in the switch case be the "try again" part, and it works if the user enters a number like 500, but as soon as a letter or character like ? is entered, I get an infinite "try again" loop.

This is the code I'm having trouble with:

#define RESTRICT(option, min, max) (option > min && option < max)

 while(!exit) {

                    printf("Choose an option:");
                    printf("1. Eliminate.");
                    printf("2. Show fraction.");
                    printf("3. Show all fractions.");
                    printf("4. Show the absolute value.");
                    printf("5. Simplify.");
                    printf("6. Add.");
                    printf("7. Subtract.");
                    printf("8. Multiply.");
                    printf("9. Divide.");
                    printf("10. Save in archive.");
                    printf("11. Load in archive.");
                    printf("12. Exit program.");

                    if(scanf("%i", &option) == 1){
                        if(RESTRICT(option,0,12)){ 
                            switch(option){
                            case 1: 
                                printf("Example");
                                break; 
                            case 2: 
                                printf("Example");
                                break; 
                            case 3:
                                printf("Example");
                                break;               
                            case 4:
                                printf("Example");
                                break; 
                            case 5:
                                printf("Example");
                                break; 
                            case 6:
                                printf("Example");
                                break; 
                            case 7:
                                printf("Example");
                                break;                    
                            case 8:
                                printf("Example");
                                break; 
                            case 9:
                                printf("Example");
                                break; 
                            case 10:
                                printf("Example");
                                break; 
                            case 11:
                                printf("Example");
                                break; 
                            }
                        } else if (option==12){
                            printf("\nGoodbye!\n");
                            exit=1;
                        } else {
                            printf("\nThat is not an option! Try again\n");
                            printf("\nBlue\n");
                            continue;
                        }
                    } else {
                                printf("\nThat is not an option! Try again\n");
                                printf("\nRed\n");
                                break;
                        }
                    }

CodePudding user response:

'a' for example, it results in an infinite loop"

'a' is never consumed by if(scanf("%i", &option) == 1){. It is not numeric text, so gets put back into stdin for the next input function. Since scanf("%i", &option) if then call again, the results repeat.

Code needs to read and consume the 'a'. Consider fgets().

Best to avoid using scanf() at all until you understand why it it bad.

CodePudding user response:

According to your code, In this code scanf("%i", &option) expects interger value from the use end. when user enter non-integer value, scanf function will not store anything in the option variable. use fgets The fgets function reads a text line or a string from the specified file or console. And then stores it to the respective string variable.

Try to update your code as follows:

char input[10];
while (!exit) {
printf("Choose an option:");
printf("1. Eliminate.");
printf("2. Show fraction.");
.
.
.
  fgets(input, sizeof(input), stdin);
  if (sscanf(input, "%i", &option) == 1) {
    if (RESTRICT(option, 0, 12)) {
      switch (option) {
      case 1:
        printf("Example");
        break;
      case 2:
        printf("Example");
        break;
        .
        .
        .
      }
    } else if (option == 12) {
      printf("\nGoodbye!\n");
      exit = 1;
    } else {
      printf("\nThat is not an option! Try again\n");
      continue;
    }
  } else {
    printf("\nThat is not an option! Try again\n");
  }
}
  • Related