Expected unqualified id-"{"

I'm trying to write a program using a menu, including 5 functions (add, sub, div, mult, and menu). I'm getting the "expected unqualified id token" message 4 times at the beginning of my add, sub, div, and mult functions. Can't seem to figure out what's wrong. Any help please?


#include<iostream>
using namespace std;

void menu();
int add();
int sub();
int div();
int mult();

void add(int a, int b);
{
cout<<"Input two numbers to add.\n";
cin>>a;
cin>>b;
cout<<"The sum is ";
int r;
r=a+b;
return r;

}


void sub(int c, int d);
{
cout<<"Input two numbers to subtract.\n";
cin>>c;
cin>>d;
cout<<"The answer is ";
int s;
s=c-d;
return s;

}


void div(int e, int f);
{
cout<<"Input two numbers to divide.\n";
cin>>e;
cin>>f;
cout<<"The answer is ";
int t;
t=e/f
return t;
}


void mult(int g, int h);
{
cout<<"Input two numbers to multiply.\n";
cin>>g;
cin>>h;
cout<<"The answer is ";
int u;
u=g*h;
return u;
}

int main()
{
int x, a, b, c, d, e, f, g, h;
char j;

cout<<"Choose a function\n";
cout<<"1. Addition\n2. Subtract\n3. Divide\n4. Multiply\n5.Quit Program";
cin>>x;

if(x==1)
{
add();
}
if(x==2)
{
sub();
}
if(x==3)
{
div();
}
if(x==4)
{
mult();
}
if(x==5)
{
return 0;
}

cout<<"Do you want to run the program again? (Y/N)\n";
cin>>j;

if(j == 'y'||j=='Y')
{
cout<<"Let's do it again!";
return main();
}
if(j=='n'||j=='N')
{
cout<<"Bye Bye!";
return 0;
}
}
closed account (oN3AqMoL)
First thing: Use the [code] function to make your code easier to read. Once you do that ill help you with the problem.

Hint: Void means you can't return a value. To return a value use int addition, multiplication, subtraction, lol. You put void for your function definitions.
Thanks for listening!
-dean :)
Last edited on
I changed the voids to int. Sorry, I'm fairly new at this. This is my first semester learning C++. What do you mean by using the function to make the code easier to read?
I was told to put the functions before the main and I thought that was easier lol.

I need it to basically output this menu:
Choose a function
1. Addition
2. Subtraction
3. Division
4. Multiplication

After that, the functions were supposed to come into play and perform the equations. Then I had a sentinel at the end to end the program if the user wanted to. It's frustrating when this program tells me something is wrong and then not to explain why or how lol.

closed account (oN3AqMoL)
(When you go to the forums theres two <> brackets. Highlight your code and then select them.)

Anyways, when you return a value, you need to assign it to an integer. It goes like this:

int return0()
{
return 0;
}

Later in int main...


integer = return0();

cout<<integer;

This output would be:
0. This is because you assigned integer the return value; 0.
If you want the addition function to be used, for example, you would do this:
int someinteger = addition(addinteger1, addinteger2)
cout<<someinteger;
Last edited on
Right. That makes sense.

so in the main... Let's say they choose 1.Addition:

if(x==1)
{
add();
}
//That's what I have.

My add() returns r.
I add the integer = add;?
That way the output would be whatever "r" is.

That was a bit confusing because my return 0; ends the program lol
And is it the cout sentences in the function that are throwing the program off? If that's the case, I can fix that up as well.
int add(int a, int b);
{
int r;
r=a+b;
return add;
}

in the main...

if(x==1)
{
cout<<"Input two numbers to add.\n";
cin>>a;
cin>>b;
cout<<"The sum is ";
add();
int r=add(int a, int b)
cout<<r;
}

I hope I've almost got the hang of this.
closed account (oN3AqMoL)
Lol I love tellign people this after Ive helped them solve a problem...

Im 12 hehehehe
@Seraph0118:

I bet you are still having a problem with your function definitions.

when you are defining your functions, do not put a semi-colon after the first line:

1
2
3
4
5
6
int add(int a, int b)   // NO SEMI-COLON HERE!
{
   int r;
   r=a+b;
   return add;
}

I can't believe I missed that. Now, I've gotten rid of those errors and new ones popped up of course. Why would it be that easy?

int add(int a, int b)
{
int r;
r=a+b;
return add; //!!Cannot initialize return object of type 'int' with an Ivalue of type 'int(int,int)'
}

in the main...

if(x==1)
{
cout<<"Input two numbers to add.\n";
cin>>a;
cin>>b;
add(int a, int b);//Expect '(' for function-style cast or type consturction
int r;
r=add(int a, int b);
cout<<"The sum is "<<r<<"\n";
}
1
2
3
4
int add(int a, int b)
{
	return (a + b);
}


Which will return the result of a + b instead of trying to return the function ;)
Now we have it working lol! Thanks all of you! Each part you guys helped me with fixed the program and the errors. Much appreciated :)
Seraph1018;

If you're lazy, you can do it like this too (only because I'm bored);
std::cout << add(a, b) << "\n";

Unless you're going to use the result for something, then you'd have to return the result into another variable in main :P
Oh no, I enjoy being lazy lol and I did just that to make it easier to read and figure out. I can't tell if it's a bad thing that it keeps encountering an internal logic error lol... It still runs though which is weird.
Last edited on
closed account (oN3AqMoL)
No problem. Sorry I couldnt have solved your questions right away, Im doing this in the middle school library.
It's fine, no worries lol.
Not to be annoying or something, but I just noticed one thing;
1
2
3
4
5
6
7
8
9
10
if(x==1)
{
cout<<"Input two numbers to add.\n";
cin>>a;
cin>>b;
add(int a, int b);//Expect '(' for function-style cast or type consturction
int r;
r=add(int a, int b);// "Error: type name not allowed"
cout<<"The sum is "<<r<<"\n";
} 

(sorry for no indentation, but too lazy to copy it into a texteditor...)

As my comment says, that won't compile. When you call a function you only write the name of the variable(s) you have to pass to the function.

So, basically you'd want to do something like this (only an example):
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int add(int a, int b)
{
	return (a + b);//return result of a + b
}

int main(void)
{
	int a = 10, b = 2;//initialize a and b
	std::cout << add(a, b) << "\n";//call add() and pass a and b to the function
}


Only in case you haven't noticed that for yourself yet ;)
No trust me, you'r not annoying or anything. Any help is welcomed lol. And yes, I did that and it is running perfectly. I'm just double checking it and adding floats so that it can handle decimals haha
Topic archived. No new replies allowed.