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;
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 :)
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.
(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;
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";
}
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.
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