Problem with functions?

I'm trying to write a calculator using functions, but I can't get it to work! The answers are not printing
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 

#include<iostream>

using namespace std;

double AddNum(int,int);
double SubNum(int,int);
double MultNum(int,int);
double DiviNum(int,int);



int main()
{

int num1, num2, selection;

cout << "Choose a number: ";
cin >> num1;

cout << "Enter a 2nd number: ";
cin >> num2;

cout << "\n\n\n";

cout << "Select the desired function:\n";
cout << "+\n ";
cout << "-\n ";
cout << "*\n ";
cout << "/\n ";

cout << "Selection: ";
cin >> selection;

cout << "\n\n\n";

}


double AddNum(int a, int b)
{
return a+b;
cout << "The sum of " << a << " and " << a << " is " << a+b << endl;
}

double SubNum (int a, int b)
{
return a-b;
cout << "The diffrence of " << 1 << " and " << b << " is " << a-b << endl;
}

double MultNum (int a, int b)
{
return a*b;
cout << a << " times " << b << " is " << a*b << endl;
}

double DiviNum (int a, int b)
{
return a/b;
cout << a << " divided by " << b << " is " << a/b << endl;

}

bool op (char a)
{
switch (a)
{
case '+': return true;
break;
case '-': return true;
break;
case '*': return true;
break;

case '/': return true;
break;

default: return false;
break;}


}


If you see anything else I did wrong or could do better, please tell. Thanks.
Last edited on
You haven't called any of your functions from main.

Also, any statement after a return statement won't be executed, returns typically go at the end of a function - from a simple point of view.

You should check for division by zero.
Last edited on
Hi there...

1
2
return a+b;
cout << "The sum of " << a << " and " << a << " is " << a+b << endl;


If you return from the function, all code below will not work(Doesn't your compiler give you warnings?).

Either you [1]cout the return value or [2]cout the answer in the function

[1]
 
cout << "The sum of " << a << " and " << a << " is " << AddNum(a,b) << endl;


OR

[2]
1
2
3
4
5
double AddNum(int a, int b)
{
cout << "The sum of " << a << " and " << a << " is " << a+b << endl;
return a+b;
}


HTH,
Aceix.
Last edited on
Topic archived. No new replies allowed.