Calculator using functions

My program is not compiling. I'm really new with functions so any help would be much appreciated. I would also like the program to spit out an answer for each of the cases. Could I just do a "cout << answer;" command for each of them?

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
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<iomanip>
#include<math.h>

using namespace std;

double Add(double a, double b);
double Sub(double a, double b);
double Mult(double a, double b);
double Div(double a, double b);
double Remain(double a, double b);
double Power(double a, double b);

double Add(double a, double b)
{
	double answer = a + b;
	return answer;
}

double Sub(double a, double b)
{
	double answer = a - b;
	return answer;
}

double Mult(double a, double b)
{
	double answer = a * b;
	return answer;
}

double Div(double a, double b)
{
	double answer = a / b;
	return answer;
}

double Remain(double a, double b)
{
	double answer = a % b;
	return answer;
}

double Power(double a, double b)
{
	double answer = a ^ b;
	return answer;
}


int main ()
{
	double num1,num2;
	int choice;
	
	cout << "Please enter a number\n";
	cin >> num1
	cout << "Please enter another number\n";
	cin >> num2
	
	cout << "Select the function you would like to use" << endl;
	
	cout << "1. Addition\n";
 	cout << "2. Subtraction\n";
	cout << "3. Multiplication\n";
	cout << "4. Division\n";
	cout << "5. Remainder\n";
	cout << "6. Power\n";
	
	cin >> choice;
	
	switch(choice)
	{
	Case 1:
		double Add(num1,num2);
		break;	
	Case 2:
		double Sub(num1,num2);
		break;
	Case 3:
		double Mult(num1,num2);
		break;
	Case 4:
		double Div(num1,num2);
		break;
	Case 5:
		double Remain(num1,num2);
		break;
	Case 6:
		double Power(num1,num2);
		break;
	
	default:
		cout << "Please select a valid function" << endl;
	}
	
}
Note:
Operator ^ doesn't do what you think it does.
In C++ the caret ^ is also spelled xor. In other words, the expression (for integral types) a ^ b takes the bits of a and b and returns a bit-mask corresponding to the bits that differ between each.

For instance, 0xAF ^ 0x5F (equivalently 175 ^ 95) will evaluate to 0xF0, or 240. (Assuming each value is unsigned)

There is no built-in exponentiation in C++. You'll have to do that yourself, or use the standard library.

% can be replaced with std::fmod for floating-point types and exponentiation can be computed with std::pow.
Last edited on
Thanks a bunch for the help! I managed to fix it.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include<iostream>
#include<iomanip>
#include<math.h>

using namespace std;

double Add(double a, double b);
double Sub(double a, double b);
double Mult(double a, double b);
double Div(double a, double b);
double Remain(double a, double b);
double Power(double a, double b);

double Add(double a, double b)
{
	double answer = a + b;
	return answer;
}

double Sub(double a, double b)
{
	double answer = a - b;
	return answer;
}

double Mult(double a, double b)
{
	double answer = a * b;
	return answer;
}

double Div(double a, double b)
{
	double answer = a / b;
	return answer;
}

double Remain(double a, double b)
{
	double answer = fmod(a,b);
	return answer;
}

double Power(double a, double b)
{
	double answer = pow(a,b);
	return answer;
}


int main ()
{
	double num1,num2,answer;
	int choice;
	
	cout << "Please enter a number\n";
	cin >> num1;
	cout << "Please enter another number\n";
	cin >> num2;
	
	cout << "Select the function you would like to use" << endl;
	
	cout << "1. Addition\n";
 	cout << "2. Subtraction\n";
	cout << "3. Multiplication\n";
	cout << "4. Division\n";
	cout << "5. Remainder\n";
	cout << "6. Power\n";
	
	cin >> choice;
	
	switch(choice)
	{
	case 1:
		answer = Add(num1,num2);
		break;	
	case 2:
		answer = Sub(num1,num2);
		break;
	case 3:
		answer = Mult(num1,num2);
		break;
	case 4:
		answer = Div(num1,num2);
		break;
	case 5:
		answer = Remain(num1,num2);
		break;
	case 6:
		answer = Power(num1,num2);
		break;
	
	default:
		cout << "Please select a valid function" << endl;
	}
	
	cout << "The answer is " << answer << "!";
	
}
WheatFieldOnFire wrote:
1. Any line of code (except for comments and strings that will be printed on the screen) should not have capital letters in them. line 74, 77, 83, 86, 89 has Case instead of case, which causes compiling problems.


It's perfectly fine to have capital letters except in reserved words (keywords, of which case is one). You should be consistent with your capitalization everywhere except comments, else your code won't compile or its meaning will change. You should pick a style convention and follow it.

Also, you can return expressions - so for each function Add, Div, Mult, etc., you can change the body to
return a + b; and so forth. No reason to type any more.

Could I just do a "cout << answer;" command for each of them?


Yes, you could -- but if you wanted to change that behavior later, you'd have to edit your code in 6 different places. There's 2 problems with that. First, it's annoying, and second, chances are you will miss one of those places and release a buggy program if nobody notices (in a large program, that's likely).

The solution is to write the print statement exactly once after collecting the result from the correct function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double result = 0.0; 
switch(choice)
	{
	case 1:
		result = Add(num1,num2);
		break;	
	case 2:
		result = Sub(num1,num2);
		break;
	case 3:
		result = Mult(num1,num2);
		break;
	case 4:
		result = Div(num1,num2);
		break;
	case 5:
		result = Remain(num1,num2);
		break;
	case 6:
		result = Power(num1,num2);
		break;
}
std::cout << "result is " << result << "\n";




Last edited on
Topic archived. No new replies allowed.