My while has no end

Asked

Viewed 105 times

1

I am making a calculator. The program performs operations depending on the number the user chooses.

The problem is that when you type 5 it is to end the loop and this is not happening.

    int main(void){
    float x,y;
    int operador;
    do{
        printf("\n\nEscolha a operacao:");
        printf("\n\t1-SOMA\n\t2-SUBTRACAO\n\t3-MULTIPLICACAO\n\t4-DIVISAO\n\t5-SAIR\n");
        scanf("%d",&operador);
        printf("Primeiro numero:");
        scanf("%f",&x);
        printf("Segundo numero:");
        scanf("%f",&y);
        switch(operador){
            case 1:
                printf("RESULTADO:%0.2f",x+y);
                break;
            case 2:
                printf("RESULTADO:%0.2f",x-y);
                break;
            case 3:
                printf("RESULTADO:%0.2f",x*y);
                break;
            case 4:
                printf("RESULTADO:%0.2f",x/y);
                break;
        }
    }while(operador!=5);
    printf("...Voce encerrou a calculadora...");
}
  • Here finished. In insert the value 5, he asked for the values of the first and second numbers, as expected and then finished the loop of repetition. What happens when you put the 5?

  • He starts again, asks to indicate the operation and the two numbers

  • Does it start again or just continue the execution as it should? For after you read the value of operador you will always read the value of x and y, regardless of the value of operador. If you type 5 to operador and any values for x and y the program will end as expected. This is not the behavior you wanted?

  • Here for me, when I type 5 for operator and any value for x e y, it does not end. He was supposed to show the last printf of the code and finish, it doesn’t happen

2 answers

2

I think you want it to work that way:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    float x,y;
    int operador;
    do{
        printf("\n\nEscolha a operacao:");
        printf("\n\t1-SOMA\n\t2-SUBTRACAO\n\t3-MULTIPLICACAO\n\t4-DIVISAO\n\t5-SAIR\n");
        scanf("%d",&operador);
        if(operador>0 && operador<=4){
            printf("Primeiro numero:");
            scanf("%f",&x);
            printf("Segundo numero:");
            scanf("%f",&y);
            switch(operador){
            case 1:
                printf("RESULTADO:%0.2f",x+y);
                break;
            case 2:
                printf("RESULTADO:%0.2f",x-y);
                break;
            case 3:
                printf("RESULTADO:%0.2f",x*y);
                break;
            case 4:
                printf("RESULTADO:%0.2f",x/y);
                break;
            }
        }
    }while(operador!=5);
    printf("...Voce encerrou a calculadora...");
    system("pause");
}

0

You have the alternative of implementing a case 5: with a continue.

You have the option to use with or without the do while, works in both ways...

        case 5:
            printf("...Voce encerrou a calculadora...");
            continue;
        }
}

or ..

        case 4:
            printf("RESULTADO:%0.2f",x/y);
            break;
        case 5:
            continue;
        }

    }while(operador!=5);
    printf("...Voce encerrou a calculadora...");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.