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!
#include <iostream>
usingnamespace 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.
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|
#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).