C++ - Stop condition in repeat structure

Asked

Viewed 1,313 times

4

How to put this code in a repeat structure so that after the calculation is shown the option "Type S to exit or C to continue"?

#include <iostream>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

using namespace std;

int main(){
setlocale (LC_ALL, "portuguese");

float soma, div, raiz, sub, escolha, numero1, numero2, pot, multi, loga;
cout << "Digite a operação desejada" << endl;
cout << "(1) Soma| X + Y" << endl;
cout << "(2) Divisão| X / Y" << endl;
cout << "(3) Raiz| RAIZ (X)" << endl;
cout << "(4) Subtração| X - Y" << endl;
cout << "(5) Potência| X^Y" << endl;
cout << "(6) Multiplicação| X * Y" << endl;
cout << "(7) Logaritmo| LOG10(X)" << endl;
cin >> escolha;




if (escolha == 1){
cout << "Digite o valor de 'X': ";
cin >> numero1;
cout << "Digite o valor de 'Y': ";
cin >> numero2;
soma = (numero1 + numero2);
cout << "X + Y = " << soma;
}else if (escolha == 2){
cout << "Digite o valor de 'X': ";
cin >> numero1;
cout << "Digite o valor de 'Y': ";
cin >> numero2;
div = numero1 / numero2;
cout << "X / Y = " << div;
}else if (escolha == 3){
cout << "Digite o valor de 'X': ";
cin >> numero1;
raiz = sqrt(numero1);
cout << "Raiz de (X) = " << raiz;
}else if (escolha == 4){
cout << "Digite o valor de 'X': ";
cin >> numero1;
cout << "Digite o valor de 'Y': ";
cin >> numero2;
sub = numero1 - numero2;
cout << "X - Y = " << sub;
}else if (escolha == 5) {
cout << "Digite o valor de 'X': ";
cin >> numero1;
cout << "Digite o valor de 'Y': ";
cin >> numero2;
pot = pow(numero1, numero2);
cout << "X^Y = " << pot;
}else if (escolha == 6){
cout << "Digite o valor de 'X': ";
cin >> numero1;
cout << "Digite o valor de 'Y': ";
cin >> numero2;
multi = numero1 * numero2;
cout << "X * y = " << multi;
}else if (escolha == 7){
cout << "Digite o valor de 'X': ";
cin >> numero1;
loga = log10(numero1);
cout << "LOG10(X) = " << loga;
}else if (escolha >7){
cout << "Você não escolheu nenhum valor";

}

cout << "\n\n";
cin.get();
system ("PAUSE");
return 0;
}

inserir a descrição da imagem aqui

  • Loop repetition with a switch. Try using while or while instead of both if/Else. See a good article for learning, if you don’t know http://www.devmedia.com.br/structura-de-repeticao-c/24121

2 answers

5


Another alternative would be to do like, how while:

#include <iostream>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

using namespace std;

int main(){
    setlocale (LC_ALL, "portuguese");

    float soma, div, raiz, sub, numero1, numero2, pot, multi, loga;
    int escolha = 0; // o case não aceita float/double/strings

    while (escolha >= 0 && escolha <= 7) {
        cout << "Digite a operação desejada" << endl;
        cout << "(1) Soma| X + Y" << endl;
        cout << "(2) Divisão| X / Y" << endl;
        cout << "(3) Raiz| RAIZ (X)" << endl;
        cout << "(4) Subtração| X - Y" << endl;
        cout << "(5) Potência| X^Y" << endl;
        cout << "(6) Multiplicação| X * Y" << endl;
        cout << "(7) Logaritmo| LOG10(X)" << endl;
        cout << "(0) Continuar" << endl;
        cout << "(?) Digite outro número para sair" << endl;
        cout << endl;
        cout << "Digite sua escolha: ";
        cin >> escolha;

        switch (escolha) {
            case 1:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                cout << "Digite o valor de 'Y': ";
                cin >> numero2;
                soma = (numero1 + numero2);
                cout << "X + Y = " << soma;
                break;
            case 2:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                cout << "Digite o valor de 'Y': ";
                cin >> numero2;
                div = numero1 / numero2;
                cout << "X / Y = " << div;
                break;
            case 3:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                raiz = sqrt(numero1);
                cout << "Raiz de (X) = " << raiz;
                break;
            case 4:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                cout << "Digite o valor de 'Y': ";
                cin >> numero2;
                sub = numero1 - numero2;
                cout << "X - Y = " << sub;  
                break;
            case 5:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                cout << "Digite o valor de 'Y': ";
                cin >> numero2;
                pot = pow(numero1, numero2);
                cout << "X^Y = " << pot;
                break;
            case 6:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                cout << "Digite o valor de 'Y': ";
                cin >> numero2;
                multi = numero1 * numero2;
                cout << "X * y = " << multi;
                break;
            case 7:
                cout << "Digite o valor de 'X': ";
                cin >> numero1;
                loga = log10(numero1);
                cout << "LOG10(X) = " << loga;
                break;
            default:
                // nada, deixa que passe para a próxima iteração para que a condição seja verificada novamente.
                break;
        }
        cout << "\n\n";
    }

    cout << "Digite qualquer tecla para sair...";
    cin.get();
    system ("PAUSE");
    return 0;
}

So you wouldn’t have to declare another variable. Also note that I initialized choice with zero so that the execution enters the loop the first time. It seems to me that you do not yet master the repeating structures, so I advise you to study a little bit about later.

Explaining, the code works like this: the code inside the while will rotate whenever the condition of the loop is true (at the end of the block, the execution goes back to condition, it is evaluated, if true, it executes the block again); the case are very suggestive, so "if the number is 1, do this", "if it is 2, do that".

An important observation is that the case may only be used with literal types, i.e., values expressed in the same code, except strings ([?] Necessary references), then some of the values that the case accept include 'a', 1, etc. Note that I modified your code by placing the var choice as int, because the switch does not accept float/double numbers, only integers.

Another thing is the breaks at the end of each block of case, you need them; otherwise the case next would be executed, indicates the end of each code case. And finally default is like the else of if, that is executed when the value is none of the above.

Just one more thing, if you want to compile on linux you should take the inclusion of conio.h.

I hope I’ve been clear and helpful.

3

In this case the most advisable structure is the {} while();, because you avoid a check that is unnecessary before the first execution of the code, since the program has a menu and this will be displayed on the screen in the first iteration of your repetition loop inevitably. To meet your problem exactly the way you posted it can create a variable to store the user’s "response" and test it in the condition of the repeat loop:

#include <iostream>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

using namespace std;

int main()
{
    setlocale (LC_ALL, "portuguese");

    float soma, div, raiz, sub, escolha, numero1, numero2, pot, multi, loga;
    char saida;

    do
    {
        cout << "Digite a operação desejada" << endl;
        cout << "(1) Soma| X + Y" << endl;
        cout << "(2) Divisão| X / Y" << endl;
        cout << "(3) Raiz| RAIZ (X)" << endl;
        cout << "(4) Subtração| X - Y" << endl;
        cout << "(5) Potência| X^Y" << endl;
        cout << "(6) Multiplicação| X * Y" << endl;
        cout << "(7) Logaritmo| LOG10(X)" << endl;
        cin >> escolha;




        if (escolha == 1)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            cout << "Digite o valor de 'Y': ";
            cin >> numero2;
            soma = (numero1 + numero2);
            cout << "X + Y = " << soma;
        }
        else if (escolha == 2)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            cout << "Digite o valor de 'Y': ";
            cin >> numero2;
            div = numero1 / numero2;
            cout << "X / Y = " << div;
        }
        else if (escolha == 3)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            raiz = sqrt(numero1);
            cout << "Raiz de (X) = " << raiz;
        }
        else if (escolha == 4)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            cout << "Digite o valor de 'Y': ";
            cin >> numero2;
            sub = numero1 - numero2;
            cout << "X - Y = " << sub;
        }
        else if (escolha == 5) 
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            cout << "Digite o valor de 'Y': ";
            cin >> numero2;
            pot = pow(numero1, numero2);
            cout << "X^Y = " << pot;
        }
        else if (escolha == 6)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            cout << "Digite o valor de 'Y': ";
            cin >> numero2;
            multi = numero1 * numero2;
            cout << "X * y = " << multi;
        }
        else if (escolha == 7)
        {
            cout << "Digite o valor de 'X': ";
            cin >> numero1;
            loga = log10(numero1);
            cout << "LOG10(X) = " << loga;
        }
        else if (escolha >7)
        {
            cout << "Você não escolheu nenhum valor";
        }

        cout << "Digite S para sair e C para continuar";
        cin >> saida;

    }while((saida != "S")||(saida != "s"));

    cout << "\n\n";
    cin.get();
    system ("PAUSE");
    return 0;
}

Browser other questions tagged

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