Trying to Design a Simple Calculator

For this specific assignment I have to use functions to make a calculator. I have to make a separate function for adding, subtracting, multiplying, and dividing. I've been trying to find some references on the internet for a couple of hours but I just can't figure this one out. Here is my code:
#include <iostream>
void add();
void sub(int n1, int n2);
int mul();
double div(double n1, double n2);
using namespace std;
int main()
{
int option;
cout << "Press 1 to add the two values together" << endl;
cout << "Press 2 to subtract the two values from eachother" << endl;
cout << "Press 3 to multiply the two values from eachother" << endl;
cout << "Press 4 to divide the two values by eachother" << endl;
cin >> option;
switch (option)
{
case '1':
int n1, n2;
cout << "Please enter two values to add" << endl;
cin >> n1 >> n2;
add();
break;
case '2':
double t1, t2;
cout << "Please enter two values to subtract" << endl;
cin >> t1 >> t2;
sub(t1,t2);
break;
case '3':
int s1, s2;
cout << "Please enter two values to multiply" << endl;
cin >> s1 >> s2;
mul();
break;
case '4':
double r1, r2;
cout << "Please enter two values to divide" << endl;
cin >> r1 >> r2;
div(r1, r2);
break;
default:
cout << "Invalid input" << endl;
}
}
void add()
{
int n1, n2, sum;
cout << "Please enter 2 numbers to add together " << endl;
cin >> n1 >> n2;
sum = n1 + n2;
cout << "The sum of these numbers is " << sum << endl;
}
void sub(double& n1, double& n2)
{
double sum;
cout << "Please enter 2 numbers to subtract" << endl;
cin >> n1 >> n2;
sum = n1 - n2;
cout << "The difference between the two numbers is " << sum << endl;
}
int mul()
{
int n1, n2, sum;
cout << "Please enter 2 numbers to multiply" << endl;
cin >> n1 >> n2;
sum = n1*n2;
cout << "The product of the two numbers is " << sum << endl;
return sum;
}
double div(double n1, double n2)
{
double sum;
cout << "Please enter 2 numbers to divide" << endl;
cin >> n1 >> n2;
sum = n1 / n2;
cout << "The quotient of the two numbers is " << sum << endl;
return sum;
}

I'm sure the problems lie somewhere in how I'm trying to apply the functions to the main block of code. I need to use the types of functions that I have already included so I can't do something like int div(int n1, int n2). I'd rather not just have somebody fix the code, I need to understand what I'm doing wrong here so I'm not completely lost on other assignments and the quiz.
First, you seem to have the right approach to the assignment, all the functions seem to be correct but I do see some errors and some useless stuff.

Second, next time post your code inside those [code ]*here[/code]. That way it will make it much easier for people to read your code.

Third, be a bit more detailed on your main problem because fixing the code is easy but I'm not sure how to direct you towards the right way if I don't know your issue.


For the Issues:

1. When using Switch/Case that are numbers, you don't close them with ' '

Bad:
1
2
3
4
case'1':
break;
case'2':
break;

Good
1
2
3
4
case 1:
break;
case 2:
break;


2. This is me being a math nerd here. Not sure how much code was given by the professor and what you need to keep but I assume every variable in this program has to be a double. There will be fractions/ decimal numbers being inputted into a calculator.

3. If you look at your prototype function of "Subtraction" The parameters don't match at all to your function delcaration. Your Prototype has int variables in its parameters but the body of the function uses double variables and passes by reference.

4. Last thing I noticed is that you are repeating cout << "Please enter 2 numbers to divide" << endl; twice, when the user inputs an option. The reason being that in the Switch/Case you are writing cout << "Please enter 2 numbers to divide" << endl; and you also have cout << "Please enter 2 numbers to divide" << endl; inside the body of the function.

Example of case
1
2
3
4
5
case '1':
int n1, n2;
cout << "Please enter two values to add" << endl;
cin >> n1 >> n2;
add();

Example of Function
1
2
3
4
5
6
7
8
void add()
{
	int n1, n2, sum;
	cout << "Please enter 2 numbers to add together " << endl;
	cin >> n1 >> n2;
	sum = n1 + n2;
	cout << "The sum of these numbers is " << sum << endl;
}


both have the cout statement causing it to state it twice. Not sure how much was given by the professor but this should be my corrections.

Switch Case:
1
2
3
case '1':
add();
break;

Function declaration:
1
2
3
4
5
6
7
8
void add()
{
	double n1, n2, sum; // changed them to double but if the professor says they have to be int, then do so.
	cout << "Please enter 2 numbers to add together " << endl;
	cin >> n1 >> n2;
	sum = n1 + n2;
	cout << "The sum of these numbers is " << sum << endl;
}





This also applies to the other Switch/Case statements and Function declarations. They all repeat the same cout statement everytime. So fix all of them


The code should be able to run with those corrections.
Last edited on
Topic archived. No new replies allowed.