Error in "break" in C ++ code prevents compilation [closed]

-4

Follow the code below:

#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.

    
asked by anonymous 11.03.2018 / 21:58

2 answers

7

I do not know since it does not have what happens wrong, but because of the code there are typos, the ; is missing 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;
}
    
11.03.2018 / 22:08
2

Just put a ; after the break.

    
23.03.2018 / 18:11