Error in "break" in C++ code prevents compilation

Asked

Viewed 104 times

-4

Follows code:

#include <iostream>

using namespace std;

int main()
{
    float n1, n2;
    int s;
    float resul;
    cout << "Digite um número para começar" << endl;
    cin >> n1;

    cout << "Vc quer (1)somar (2)diminuir (3)mutiplicar (4)dividir" << endl;
    cin >> s;
    switch (s) {
    case 1:
        cout << "Com qual valor?" << endl;
        cin >> n2;
        resul = n1 + n2;
        break case 2 : cout << "Com qual valor?" << endl;
        cin >> n2;
        resul = n1 - n2;
        break case 3 : cout << "Com qual valor?" << endl;
        cin >> n2;
        resul = n1 * n2;
        break case 4 : cout << "Com qual valor?" << endl;
        cin >> n2;
        resul = n1 / n2;
        break default : cout << "Error 404" << endl;
    }

    cout << "O resultado é " << resul << endl;

    return 0;
}

Where did I go wrong? It’s a simple calculator.

  • 2

    What is the doubt?

  • was giving error, but I discovered that it was because I had forgotten to put the ; after the Break hehe

2 answers

7


I don’t know if you don’t have what’s wrong, but the code says typos, missing the ; after each break.

Organizing the code a little better would look like this:

#include <iostream>
using namespace std;

int main() {
    float n1, n2, resultado;
    int s;
    cout << "Digite um número para começar";
    cin >> n1;
    cout << endl << "Vc quer (1)somar (2)diminuir (3)mutiplicar (4)dividir";
    cin >> s;
    cout << endl << "Com qual valor?";
    cin >> n2;
    switch(s) {
    case 1:
        resultado = n1 + n2;
        break;
    case 2:
        resultado = n1 - n2;
        break;
    case 3:
        resultado = n1 * n2;
        break;
    case 4:
        resultado = n1 / n2;
        break;
    default:
        cout << endl << "Error 404";
        return 0;
    }
    cout << endl << "O resultado é " << resultado << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • kkk That msm, I am very forgotten. vlw^^

2

Just put a ; after the break.

  • in front or after?

  • what your reply adds to what has already been said in the other reply?

Browser other questions tagged

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