Syntax Error

Hi. I'm making my first program. It's a simple calculator. I'm running it in Code::Blocks with a GNU GCC compiler on a Mac.
The code is:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

int main()
{
    float x;
    float y;
    char choice;

    cout <<"Enter a number (x)"<<endl;
    cin>>x;
    cout <<"Enter another number (y)"<<endl;
    cin>>y;
    cout <<"What function would you like? \n / \n * \n + \n - \n ^";
    cin>>choice;
    switch(choice)
            {case '+' : cout <<x+y;
                break;
            case '-' : if (y>x)
                cout <<"Would you like a positive number? 0 is 'Yes', 1 is 'No'.";
                    if
                        {cin>>0
                        cout <<y-x;
                    else
                        {cout <<x-y;
                    else
                        cout <<x-y;}}
            case '*' : cout <<x*y;
            case '/' : cout <<"Would you like to do x/y or y/x? \n If x/y enter 0. If y/x enter 1."
                    if cin>>0
                        cout <<x/y;
                    else
                        {if cin>>1
                            cout <<y/x;
                        else
                            cout <<"Nice try.";}
            case '^' : cout <<"Do you want x^y or y^x? \n If x^y enter 0. If y^x enter 1."

                    if cin>>0
                        cout <<x^y;
                    else
                        if cin>>1
                            cout <<y^x;
                        else
                            cout <<"Nice try.";

       cout <<"Do you want to perform another function? Yes or No."
       cin<<ans
            if cin<<Yes
                return 0;
            else
            {[[noreturn]] void _Exit ( int status ) noexcept;}
   }
}


When I build it, the compiler gives me three errors. On line 23, it says "error: expected '(' before '{' token. Then, it gives me another for line 55, saying, "error: expected '}' at end of input". Somehow, this error shows up again, saying the exact same thing for the same line. I think this is just a simple syntax error, but I could be wrong.






You have to place a condition inside parentheses after if. And when you put a { inside an if you have to place the } before you put the else.
You have multiple places where you do stuff like if cin >> 0
What you're trying to do is compare the input to 0, but you can't do it like this. It needs to be something like:
1
2
3
cin >> temp;
if (temp == 0)
 //do stuff 


Also, you seem to just randomly throw braces around. For instance:
1
2
3
4
5
6
7
if
                        {cin>>0
                        cout <<y-x;
                    else
                        {cout <<x-y;
                    else
                        cout <<x-y;}}

While it contains the problem with if statements above, it also incorectly uses braces. It also has an else right after another else, which doesn't work. Fixing all the problems, it should be:
1
2
3
4
5
6
7
8
9
cin >> temp;
if (temp == 0)
{
    cout << y - x;
}
else
{
    cout << x - y;
}

Note that if the code you want associated with each if or else block is just 1 line, you don't need the braces at all.

Try to clean up the code and repost it once you get some new errors.
Last edited on
Topic archived. No new replies allowed.