Help Needed

I am trying to make a simple calculator program, where I ask the user if they would like to add, subtract, multiply, or divide. But no matter what they choose it prints all four of the answers. Here is the code.

#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int answer;
int sum;
sum = a + b;
int difference;
difference = a - b;
int product;
product = a * b;
int quotient;
quotient = a / b;

cout << "Type a number! \n";
cin >> a;

cout << "Type another number! \n";
cin >> b;

cout << "Would you like to add, if so type 1, subtract, if so type 2, multiply, if so type 3, or divide, if so type 4.";
cin >> answer;

if(answer == 1)
{
cout << "Okay \n";
cout << "The sum of your numbers is " << sum << endl;
}

else if(answer == 2)
{
cout << "Okay \n";
cout << "The difference of your numbers is " << difference << endl;
}

else if(answer == 3)
{
cout << "Okay \n";
cout << "The product of your numbers is " << product << endl;
}

else if(answer == 4)
{
cout << "Okay \n";
cout << "The quotient of your numbers is " << quotient << endl;
}

else
{
cout << "You appear to have not typed a number 1 - 4, please try again.";
cin >> answer;
}


return 0;
}
try this:

#include <iostream>
using namespace std;
double add(double, char, double);
double subt(double, char, double);
double times(double, char, double);
double divide(double, char, double);
int main ()
{
char oper;
double num1, num2, result;
cout << "Hello, and welcome to the calculator program.\n";
do {
cout << "Please enter your equation\n";
cin >> num1 >> oper >> num2;
if (oper != '+' && oper != '-' && oper != '*' && oper != '/')
cout << "ERROR\n";
else
cout << num1 << oper << num2 << "=";
if (oper == '+')

cout << add(num1, oper, num2);

if (oper == '-')

cout << subt(num1, oper, num2);

if (oper == '*')

cout << times(num1, oper, num2);

if (oper == '/')

cout << divide(num1, oper, num2);
cout << endl;
cout << "If you would like to continue, please press 1.\n";
cin >> result;
} while (result == 1);

}

double add(double num1, char oper, double num2)
{
double result;
result = num1 + num2;
return result;
}
double subt(double num1, char oper, double num2)
{
double result;
result = num1 - num2;
return result;
}
double times(double num1, char oper, double num2)
{
double result;
result = num1 * num2;
return result;
}
double divide(double num1, char oper, double num2)
{
double result;
result = num1 / num2;
return result;
}
I was looking for something a bit more simple, I am more of a beginner programmer.
This program is very simple hahaha, The thing that may be a bit confusing for beginners are the functions. but you can just put this all in main if you would like
All of the operations are characterized as chars which is why you need the single quote mark before each one.
Thanks, for all of your help, :D

I have been looking at the program you sent me and I can understand how it is formatted. I will start working from there, thanks again.
Here's a much simpler answer:

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
#include <iostream>

using namespace std;

int main(){

	char op;
	double num1, num2;

	cout << "Enter the equation ( x + y, x - y etc.): \n";
	cin >> num1 >> op >> num2;
	
	double result;

	switch (op){

	case '+': result = num1 + num2;
		break;
	case '-': result = num1 - num2;
		break;
	case '/': result = num1 / num2;
		break;
	case '*': result = num1 * num2;
		break;

	default: cout << "Invalid operator has been entered" << endl;
	}

	cout << "\nThe result is: " << result << endl;

	system("PAUSE");

}
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
/* 	This is a really simple calculator you're more then 
 * 	welcome to use this or simply build off of it, it may look complicated 
 * 	but it really isn't!
 
 */


#include <iostream> 
using namespace std; 

//global variables. in this case I think it's easier if I do it this way it should seem more clear to ya.
int input;
int NumberOne;
int NumberTwo;

//The following are the operational functions called in the switch statement
int addition(int a, int b)
{ 
int sum;
sum=a+b; 
return sum;  
} 

int subtraction(int a,int b) 
{ 
int sum;
sum=a-b; 
return sum; 
} 

int multiplication(int a,int b) 
{ 
int sum; 
sum=a*b; 
return sum; 
} 

int division(int a,int b) 
{ 
int sum; 
sum=a/b; 
return sum;  
}


int main() 
{  

    cout << " 1 - addition:\n 2 - subtraction:\n 3 - multiplication:\n 4 - division: " << endl; 
    cin>>input;
    switch (input)  
{  
 
case 1:   cout << "Enter First Number"; 
	  cin >> NumberOne; 
	  cout << " Enter Second Number"; 
	  cin >> NumberTwo;
	  cout <<NumberOne << "+" << NumberTwo << "=" << addition(NumberOne,NumberTwo)<<endl;
	  break; 
	  
case 2:   cout << "Enter First Number"; 
	  cin >> NumberOne; 
	  cout << " Enter Second Number"; 
	  cin >> NumberTwo; 
	  cout << NumberOne <<"-" << NumberTwo << "=" << subtraction(NumberOne,NumberTwo)<<endl;
	  break;
  
case 3:   cout << "Enter First Number";
	  cin >> NumberOne; 
	  cout << " Enter Second Number"; 
	  cin >> NumberTwo;
	  cout << NumberOne << "*" << NumberTwo << "=" << multiplication (NumberOne,NumberTwo)<<endl;
	  break;   
  
  
case 4:   cout << "Enter First Number"; 
	  cin >> NumberOne; 
	  cout << " Enter Second Number"; 
	  cin >> NumberTwo; 
	  cout << NumberOne << "/" << NumberTwo << "=" << division(NumberOne,NumberTwo) <<endl;
	  break;  

	  
	  
}  
   
 
 
return 0; 
 }  

/*I was going to throw some loops into it but since you're looking for some thing simple I figured this might help you out*/
Last edited on
closed account (jh5jz8AR)
Hey BadgerBrig,

I am new to coding as well. I took your code and made a few changes so you can see what I did to make your code work.

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
#include <iostream>

using namespace std;

int main()
{
	int a;
	int b;
	int answer;
	int sum;
	int difference;
	int product;
	int quotient;

	cout << "Type a number! \n";
	cin >> a;

	cout << "Type another number! \n";
	cin >> b;

	cout << "Would you like to add, if so type 1, subtract, if so type 2, multiply, if so type 3, or divide, if so type 4.";
	cin >> answer;

	if (answer == 1)
	{
		cout << "Okay \n";
		sum = a + b;
		cout << "The sum of your numbers is " << sum << endl;
	}
	else if (answer == 2)
	{
		cout << "Okay \n";
		difference = a - b;
		cout << "The difference of your numbers is " << difference << endl;
	}
	else if (answer == 3)
	{
		cout << "Okay \n";
		product = a * b;
		cout << "The product of your numbers is " << product << endl;
	}
	else if (answer == 4)
	{
		cout << "Okay \n";
		quotient = a / b;
		cout << "The quotient of your numbers is " << quotient << endl;
	}
	else
	{
		cout << "You appear to have not typed a number 1 - 4, please try again.";
		cin >> answer;
	}
	system("PAUSE");
	return 0;
}


I moved the following 4 lines of code:

[code]
sum = a + b;
difference = a - b;
product = a * b;
quotient = a / b;

The reason you were having an issue was because you were trying to assign those 4 pieces of code a value before gathering the values of a and b. I hope this is clear enough for you to understand.

Like you, I am fairly new to programming too.

Happy coding badgerBrig.
Thanks everyone for the great suggestions.
Topic archived. No new replies allowed.