Function with return values

Jan 13, 2014 at 12:27pm
Can someone help me with this code. I don't understand the error in this code. Thanks in advance. :)

#include<iostream.h>
#include<conio.h>
using namespace std;

int add(int x, int y);
int subtract(int x, int y);
int multiply(int x, int y);
int divide(int x, int y);

int main(){
int x,y;
cout<<"Enter 1st Number: "<<endl;
cin>>x;
cout<<"Enter 2nd Number: "<<endl;
cin>>y;
execute(x,y);
getch();
return 0;
}

int add(int x, int y){
int sum;
sum=x+y;
return (sum);
}
int subtract(int x, int y){
int diff;
diff=x-y;
return (diff);
}
int multiply(int x, int y){
int prod;
prod=x*y;
return (prod);
}
int divide(int x, int y){
int quotient;
quotient=x/y;
return (quotient);
}
int result(int r){
cout<<"Result: "<<r;
}

int execute(int b, int c){
char select;
int a, s, m, d;
system("cls");
cout<<"Menu"<<endl;
cout<<"[A]ddtion"<<endl;
cout<<"[S]ubtraction"<<endl;
cout<<"[M]ultiplication"<<endl;
cout<<"[D]ivision"<<endl;
cout<<"Select: ";
cin>>select;
switch(select){
case 'a':
a=add(x,y);
result (a);
break;
case 's':
s=subtract(x,y);
result (s);
break;
case 'm':
m=multiply(x,y);
result (m);
break;
case 'd':
d=divide(x,y);
result (d);
break;
default:
cout<<"Please Check Your Answer";
}
return 0;
}


Jan 13, 2014 at 12:58pm
change
#include<iostream.h>
to
#include<iostream>

then you'll get a bunch of other errors to fix.
e.g.
a=add(x,y);

you havent defined what x and y are (perhaps you meant b and c?).

also you need to declare execute before calling it.


also use code tags please when you post code.
Last edited on Jan 13, 2014 at 12:59pm
Jan 13, 2014 at 1:14pm
This isn't an error, but the code could be simpler:
1
2
3
4
5
6
int subtract(int x, int y)
{
    int diff;
    diff = x - y;
    return (diff);
}

could be replaced with:
1
2
3
4
int subtract(int x, int y)
{
    return x - y;
}

and the same applies to the other similar functions (though in the case of divide() you may want to add a check to avoid dividing by zero).
Last edited on Jan 13, 2014 at 1:14pm
Jan 13, 2014 at 1:19pm
You are missing the prototype of execute(), the compiler doesn't know it yet when you call it in main()

change int result( int r ) to void result( int r ) since the function doesn't return anything

In function execute() :
a = add( x, y ), s = subtract( x, y )etc...
change those to : a = add( b, c ); s = subtract( b, c ); etc..


-*-*- [EDIT] -*-*-

you're a filipino (filipina) ? san ka nag - aaral ?
Last edited on Jan 13, 2014 at 1:58pm
Jan 13, 2014 at 1:50pm
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
int execute(int b, int c){
char select;
int a, s, m, d;
system("cls");
cout<<"Menu"<<endl;
cout<<"[A]ddtion"<<endl;
cout<<"[S]ubtraction"<<endl;
cout<<"[M]ultiplication"<<endl;
cout<<"[D]ivision"<<endl;
cout<<"Select: ";
cin>>select;
switch(select){
case 'a':
a=add(x,y);
result (a);
break;
case 's':
s=subtract(x,y);
result (s);
break;
case 'm':
m=multiply(x,y);
result (m);
break;
case 'd':
d=divide(x,y);
result (d);
break;
default:
cout<<"Please Check Your Answer";
}
return 0;
}


Define x , y first the easy way is do it in main.
u cont call one function var in anther unless it static
Jan 14, 2014 at 12:36am
Thank you very much for your help. :))
Jan 14, 2014 at 1:10am
Hi Catherine,

You probably don't want to do integer division either, because it truncates the decimal portion: 3 / 6 is zero.

So you could change the type of of all your variables to double.

You still need to check for zero, try it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <cmath>

bool IsZero (double number, double PRECISION); // declaration

const double PRECISION = 0.001; // declaration inside main

bool IsZero (double number, double PRECISION) {
  if (abs(number) < PRECISION) {
     return true;
  }
  else {
     return false;
  }
}


You can use the ?: operator for simple if then else constructs - have a look in the tutorial page at the top left of this page.

number < PRECISION ? true : false;

Also, can you please use code tags? Select your code then press the <> button on the formatting menu. Your code should look like the code posted by me & others.

Hope all goes well :)
Last edited on Jan 14, 2014 at 1:14pm
Topic archived. No new replies allowed.