Codeblocks Help! Unqualified ID!

Basically I have to write arithmetic functions in codeblocks, basic stuff. I'm a starter in C++, this is for my class. Issue is, I have to just input different equations, like simple addition, subtraction etc, but I don't know how to do it in the same program. So I tried this, but its failing. I don't know how to do several functions/equations in the same program... I tried this, but I got this error. All help would be nice, thanks!!

Error code when I try running: C:\Users\Public\Documents\Assignment #1\main.cpp|20|error: expected unqualified-id before '{' token|

The first, 8+9 works, but when I add a new function, it screws up!
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
#include <iostream>

using namespace std;

int main()
{

 int numb1, numb2, ans;
     numb1 = 8;
     numb2 = 9;
     ans = numb1 + numb2
;{

}
    cout << "The sum is" << ans << endl;

    return 0;
}

{
   int numb1, numb2, ans;
     numb1 = 22;
     numb2 = 9;
     ans = numb1 + numb2
;{

}
    cout << "The sum is" << ans << endl;

    return 0;
}
Last edited on
You have stuff outside of your main function where you shouldn't have anything.
Perhaps a little better indentation will help:
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
#include <iostream>

using namespace std;

int main()
{
    int numb1, numb2, ans;
    numb1 = 8;
    numb2 = 9;
    ans = numb1 + numb2;
    {
        // Err...what's the purpose of these curly braces? They don't do anything
    }
    cout << "The sum is" << ans << endl;

    return 0;
}
// This stuff below is outside of main()
{
    int numb1, numb2, ans;
    numb1 = 22;
    numb2 = 9;
    ans = numb1 + numb2;
    {

    }
    cout << "The sum is" << ans << endl;

    return 0;
}

Seems like if you just remove lines 19-30 above, you should be fine.
Last edited on
what is the point of the empty block on lines 13-15?

also most of your program isn't inside any function.
I dont want to remove it.. I need to repeat the same functions, but I have to do different equations... How do I get several functions in the same program?
#include <iostream>

using namespace std;

int main()
{

 int numb1, numb2, ans;
     numb1 = 8;
     numb2 = 9;
     ans = numb1 + numb2
    ;cout << "The sum is" << ans << endl;

    return 0;
}
{

 int numb1, numb2, ans;
     numb1 = 22;
     numb2 = 9;
     ans = numb1 + numb2
    ;cout << "The sum is" << ans << endl;

    return 0;
}


There, I got rid of excess lines. Point I'm trying to make is that I'm trying to replicate the first function 8+9! But it isn't working, says: C:\Users\Public\Documents\Assignment #1\main.cpp|16|error: expected unqualified-id before '{' token|
The problem is that you can't write code that's outside of any function.

If you haven't learned about functions yet, you'll just have to go with something like
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "8 + 9 = " << 8 + 9 << std::endl;
    std::cout << "22 + 9 = " << 22 + 9;
    // etc.
}

or if you really need to use variables:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int num1, num2, sum;
    num1 = 8;
    num2 = 9;
    sum = num1 + num2;
    std::cout << "Sum: " << sum << std::endl;

    num1 = 22; // Change the value of num1
    sum = num1 + num2;
    std::cout << "New sum: " << sum;
}


If you're a big fan of copy/paste, you could even do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    {
        int num1, num2, sum;
        num1 = 8;
        num2 = 9;
        sum = num1 + num2;
        std::cout << "Sum: " << sum << std::endl;
    } // All of those variables that we declared above disappear here
    {
        // Here's a different one
        int num1, num2, sum;
        num1 = 22;
        num2 = 9;
        sum = num1 + num2;
        std::cout << "Sum: " << sum;
    }
}

but I wouldn't recommend that (too much redundancy).
Last edited on
look here for info on functions: http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.