calculator

only addition is working...

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
 #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int num1 = 0;
	int num2 = 0;
	int way = 0;
	int answer = 0;

	cout << "Hello there this is a calculator built by Paul Maywald..." << endl;
	cout << "This is a simple calculator that divedes, multiplies, adds, and subtracts..." << endl;
	cout << "Enter a number beween 1 and 4 to choose what you would like to do..." << endl;
	cout << "Enter your first number please: " << endl;
	cin >> num1;
	cout << "Enter your second number please: " << endl;
	cin >> num2;
	cout << "1 is to add, 2 is to subtract, 3 is to multiply, and 4 is to divide..."<< endl;
	cin >> way;
		while(way != 5)
		{
			switch(way)
		{
			case 1:
				answer = num1 + num2;
		cout << "Heres the answer: " <<  answer <<endl;
		cin >> answer;
		break;
			case 2:
				if(num1 > num2)
		{
			answer = num1 - num2;
			cout << "Heres the answer: " << answer << endl;
			cin >> answer;
		}
		else if(num2 > num1)
		{
			answer = num2 - num1;
			cout << "Heres the answer: " << answer << endl;
			cin >> answer;
		}
				break;
			case 3:
				answer = num1 * num2;
		cout << "Heres the answer: " << answer << endl;
		cin >> answer;
		break;
			case 4:
				if(num1 > num2)
		{
			answer = num1/num2;
			cout << "Heres the answer: " << answer << endl;
			cin >> answer;
		}
		else if(num2 > num1)
		{
			answer = num2/num1;
			cout << "Heres the answer: " << answer << endl;
			cin >> answer;
		}
		}
		}
		cout << "Enter -0 to quit" << endl;


	system("pause");
	return 0;
}
my bad only subtraction doesn't work...
What values are you putting in that are causing specific trouble with subtraction? I can see there being trouble there (and in division) if the two numbers entered are the same.

I'm curious why you are prompting for user input into the answer variable right after you output the solution?
1
2
cout << "Heres the answer: " << answer << endl;
cin >> answer;


The calculator is running in an infinite loop because there's no way to make the while condition statement while(way != 5) to equal 5. Edit - do you mean for them to be able to enter new numbers and do another calculation?

Another thought on division - with integer values you're going to get integer division, which may not be the result the user is expecting. And checking to see what number is larger to determine how you do the division may not be what the user is expecting either. If the first number I enter is 1 and the second 3 - I'm expecting 1/3 = .33333 etc. The code right now would give me 3/1 = 3.
Last edited on
closed account (j3Rz8vqX)
They all seem to work from my perspective, except when you subtract or divide using equal values.

a = 5
b = 5

a not greater or less than b.

Case 2 and 4 would do nothing for equal inputs.

Also, what particular problem are you having for subtraction?
i got it all fixed thanks guys i really appreciate it and if you have any program i could try and make tell me please
Topic archived. No new replies allowed.