'{' Error! Please Help!

Dear Members, I have recently started learning C++ on my own. I have learned fast and have improved my skills. Today I decided to make a simple calculator but have encountered some errors. I have tried searching a lot but have failed to find my error (due to lack of experience) and have thus turned for your help.

The errors I get are:
- expected `,' or `;' before '{' token
- a function-definition is not allowed here before '{' token

Here is my code:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<string>
void addn (int a, int b);
void subn (int a, int b);
void muln (int a, int b);
void divn (int a, int b);
int n,a,b,ans;
using namespace std;
int main()
{
    cout<<"Welcome to iCalc 1.0 !"<<endl;
    loop:
    do {
        cout<<"Please enter a number to choose operation (Enter 0 to exit):"<<endl<<endl;
        cout<<"1. addnition"<<endl;
        cout<<"2. Subtraction"<<endl;
        cout<<"3. Multiplication"<<endl;
        cout<<"4. Division"<<endl;
        cout<<"5. About"<<endl<<endl;
        cout<<"NUMBER:\t";
        cin>>n;
        } while (n!=0);
        
        if (n>5){
                 cout<<"Please enter a number from 1-5 OR enter 0 to exit";
                 goto loop;
                 }
        else
        switch (n) {
               case 1:
                    cout<<"Please enter 1st number to addn:\t";
                    cin>>a;
                    cout<<endl<<"Please enter 2nd number to addn: \t";
                    cin>>b;
                    addn (a, b);
                    break;
               case 2:
                    cout<<"Please enter 1st number to subtract:\t";
                    cin>>a;
                    cout<<endl<<"Please enter 2nd number to subtract: \t";
                    cin>>b;
                    subn (a, b);
                    break;
               case 3:
                    cout<<"Please enter 1st number to multiply:\t";
                    cin>>a;
                    cout<<endl<<"Please enter 2nd number to multiply: \t";
                    cin>>b;
                    muln (a, b);
                    break;
               case 4:
                    cout<<"Please enter 1st number to divide:\t";
                    cin>>a;
                    cout<<endl<<"Please enter 2nd number to divide: \t";
                    cin>>b;
                    divn (a, b);
                    break;
               case 5:
                    cout<<"VERSION:\t1.0\n\n";
                    break;
               default:
                       cout<<"Please enter a number from 1-5 OR enter 0 to exit";
                       goto loop;
                       }
void addn (int a, int b)
{
     a+b = ans;
     cout<<ans;
     goto loop;
}

void subn (int a, int b)
{
     a-b = ans;
     cout<<ans;
     goto loop;
}

void muln (int a, int b)
{
     a*b = ans;
     cout<<ans;
     goto loop;
}     

void divn (int a, int b)
{
     a/b = ans;
     cout<<ans;
     goto loop;
}                    
       
    system ("pause");
    return 0;
}


Please help me out!

Neil010

Last edited on
Your primary issue is that you've made function within main(). Functions cannot be defined inside of other functions.

Other issues: a = b means that a gets the value of b. You had a-b = ans. That means a-b gets the value of ans which is invalid.

Your gotos gave errors. As a general rule of thumb don't use goto. It makes unreadable and un-debuggable code. Especially when things get complicated. If you use these, you instantly limit the number of people willing to read your post. I've never come across a situation where structured programming cannot achieve the same thing.

It's best to avoid global variables as you can't protect them from being mistakenly written to in other functions and name conflicts resulting in ambiguous behavior might happen. Avoid these when possible. In order to pass data between functions, you should pass this using arguments and return codes.

Here is your code edited and with comments:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>
#include<string>
void addn (int a, int b);
void subn (int a, int b);
void muln (int a, int b);
void divn (int a, int b);

using namespace std;
int main()
{
	int n,a,b; // These didn't need to be global
    cout<<"Welcome to iCalc 1.0 !"<<endl;
    loop:
    do {
        cout<<"Please enter a number to choose operation (Enter 0 to exit):"<<endl<<endl;
        cout<<"1. addnition"<<endl;
        cout<<"2. Subtraction"<<endl;
        cout<<"3. Multiplication"<<endl;
        cout<<"4. Division"<<endl;
        cout<<"5. About"<<endl<<endl;
        cout<<"NUMBER:\t";
        cin>>n;
    } while (n!=0);	// Indenting, this should be aligned with the do
        
    if (n>5){
             cout<<"Please enter a number from 1-5 OR enter 0 to exit";
             goto loop;
    } // Indenting, it should be aligned with the if
    else 
	{ // added to make the 'else' scope clearer
        switch (n) {
            case 1:
                 cout<<"Please enter 1st number to addn:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to addn: \t";
                 cin>>b;
                 addn (a, b);
                 break;
            case 2:
                 cout<<"Please enter 1st number to subtract:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to subtract: \t";
                 cin>>b;
                 subn (a, b);
                 break;
            case 3:
                 cout<<"Please enter 1st number to multiply:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to multiply: \t";
                 cin>>b;
                 muln (a, b);
                 break;
            case 4:
                 cout<<"Please enter 1st number to divide:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to divide: \t";
                 cin>>b;
                 divn (a, b);
                 break;
            case 5:
                 cout<<"VERSION:\t1.0\n\n";
                 break;
            default:
                 cout<<"Please enter a number from 1-5 OR enter 0 to exit";
                 goto loop;
        } // I changed the indenting of this, it closes the switch   
    } // I added this to make the 'else' scope clearer
    system ("pause");
    return 0;
}

// Function definitions are no longer in main
void addn (int a, int b)
{
     int ans = a+b;	// you can make ans a local variable.
     cout<<ans;
//   goto loop;  You cannot goto something in another function
}

void subn (int a, int b)
{
     int ans = a-b; // The variable on the left is what will get the value, a-b = ans doesn't work.
     cout<<ans;
//   goto loop;
}

void muln (int a, int b)
{
     int ans = a*b;
     cout<<ans;
//   goto loop;
}     

void divn (int a, int b)
{
     int ans = a/b;
     cout<<ans;
//   goto loop;
} 
Last edited on
Your code above compiles, but I couldn't help noticing a major flaw. The do-while will continue to loop unless 0 is entered. When you've entered 0, it will then run the rest of the code which is set to exit when we see 0.

I've gotten rid of the goto by adding a loop instead and I've corrected your logical issue:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<string>
void addn (int a, int b);
void subn (int a, int b);
void muln (int a, int b);
void divn (int a, int b);

using namespace std;
int main()
{
    int n = 1,a,b; // These didn't need to be global, n is initialized to not 0;
    cout<<"Welcome to iCalc 1.0 !"<<endl;
    while (n != 0) // loop: is replaced with a real structured loop.
    {
        do {
            cout<<"Please enter a number to choose operation (Enter 0 to exit):"<<endl<<endl;
            cout<<"1. addnition"<<endl;
            cout<<"2. Subtraction"<<endl;
            cout<<"3. Multiplication"<<endl;
            cout<<"4. Division"<<endl;
            cout<<"5. About"<<endl<<endl;
            cout<<"NUMBER:\t";
            cin>>n;

            if (n > 5) // I moved this if into the loop which checks for validity
            {
                cout << "Please enter a number from 1-5 OR enter 0 to exit" << endl; // You were missing endl
            }
        } while (n > 5);    // Indenting, this should be aligned with the do. Condition changed to "while n is invalid"
        
        switch (n) {
            case 1:
                 cout<<"Please enter 1st number to addn:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to addn: \t";
                 cin>>b;
                 addn (a, b);
                 break;
            case 2:
                 cout<<"Please enter 1st number to subtract:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to subtract: \t";
                 cin>>b;
                 subn (a, b);
                 break;
            case 3:
                 cout<<"Please enter 1st number to multiply:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to multiply: \t";
                 cin>>b;
                 muln (a, b);
                 break;
            case 4:
                 cout<<"Please enter 1st number to divide:\t";
                 cin>>a;
                 cout<<endl<<"Please enter 2nd number to divide: \t";
                 cin>>b;
                 divn (a, b);
                 break;
            case 5:
                 cout<<"VERSION:\t1.0\n\n";
                 break;
            // The default case wasn't required because it's all handeled by the stuff at the top of the loop
        } // I changed the indenting of this, it closes the switch   
    } // Closes the while loop (MUCH better than a goto)

    system ("pause");
    return 0;
}

// Function definitions are no longer in main
void addn (int a, int b)
{
     int ans = a+b;    // you can make ans a local variable.
     cout<<ans;
//   goto loop;  You cannot goto something in another function
}

void subn (int a, int b)
{
     int ans = a-b; // The variable on the left is what will get the value, a-b = ans doesn't work.
     cout<<ans;
//   goto loop;
}

void muln (int a, int b)
{
     int ans = a*b;
     cout<<ans;
//   goto loop;
}     

void divn (int a, int b)
{
     int ans = a/b;
     cout<<ans;
//   goto loop;
} 
Last edited on
Topic archived. No new replies allowed.