Weird switch error

Hello, noob here. Been trying to make a calculator but i keep getting the same error (Error: expected ':' before cout). This is reported on the switch section. I am using Code Blocks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>;

using namespace std;

main()
{
    double var1 , var2;
    char decision;
    cout<< "Enter first number: "<< endl;
    cin>> var1;
    cout<< "Enter second number:"<< endl;
    cin>> var2;

    cout<< "What do you want to do with that number? ";
     cout<< "+ add"<< endl;
    cout<< "- substract"<< endl;
    cout<< "* multiply "<< endl;
    cout<< "/ divide"<< endl;
    cin >> decision;


    switch (decision)
    {
       case '+'
            cout << var1 << " + " << var2<< " = "<< (var1 + var2)<< endl;
        break;
    case '-'
            cout << var1 << " - " << var2<< " = "<< (var1 - var2)<< endl;
        break;
    case '*'
            cout << var1 << " * " << var2<< " = "<< (var1 * var2)<< endl;
        break;
    case '+'
            cout << var1 << " / " << var2<< " = "<< (var1 / var2)<< endl;
        break;
    default;
    cout <<"You typed wrong character!!";
    }

}
http://www.cplusplus.com/doc/tutorial/control/

Near the bottom there is the switch statement, always look up the documentation for the things you are using.

main must return an int:

1
2
3
int main() {

}


Always initialise your variables to something.

When doing division (you aren't - that's a clue to another problem) , always check for division by zero. It looks like this for a double value:

1
2
3
4
5
6
7
8
constant double PRECISION = 1 e-5;  // set this to whatever suits you
double a = 0.0;  // 1 variable per line, initilised
double b = 0.0;

// get input

if (b < PRECISION) {std::cout << "B is near enough to zero\n";}
else {double answer = a / b;}


Here's a clue how to discover problems - use the compiler with all the warnings turned on. Try cpp.sh (the gear icon top right of the code) turn on all 3 warnings on the options tab
Last edited on
Hello gadamidis1,

Actually the error is not that weird. Keep in mind that an error reported on one is most often on the line above. Hint "default" comes the closest to being written correctly, but even it is wrong.

As the TheIdeasMan said use the gear icon in the top right corner of your code. The errors that are returned are very good.

Look for the error on line 1.

"using namespace std;" is a bad idea. Do a search here and read up on this. This is also useful:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Hope that helps,

Andy
Topic archived. No new replies allowed.