Calculator help

Oct 23, 2014 at 10:05am
Hi, I wrote this calculator code, but when I run it, as soon as I input the operation, it prints the two questions of whichever operation I chose (asking you to input numbers) and then gives a random answer. If someone could explain what I did wrong and why it produced that, and how I can fix the code, it would be a big help. Thanks.

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

using namespace std;
int main ()
{

enum Operation
{
OPERATION_SUBTRACTION,
OPERATION_ADDITION, OPERATION_DIVISION,
OPERATION_MULTIPLICATION
};

	int Operation = 1 ;
cout << "Which operation would you like to use? " ;
cin >> Operation; 

switch (Operation)
{


			
case OPERATION_SUBTRACTION:
{	
int x;
cout << "What is the number you would like to subtract from?"<< endl;
cin >> x;
int y;
cout << " What is the number you would like to subtract?" << endl;
cin >> y;
int difference = x - y;
cout << " The difference is " << difference <<"." << endl;
break;
} 
case OPERATION_ADDITION:
{
int x;
cout << "What is the first number you would like to add?"<< endl;
cin >> x;
int y;
cout << " What is the second number you would like to add?" << endl;
cin >> y;
int sum = x + y;
cout << " The sum is " << sum <<"." << endl;
break;
} 
case OPERATION_DIVISION:
{
int x;

cout << "What is the dividend?"<< endl;
cin >>x ;
int y;
cout << " What is the divisor??" << endl;
cin >> y; 
int quotient = x/y ;
cout << "The quotient is " << quotient << "." << endl;
break;
} 
case OPERATION_MULTIPLICATION:
{
int x;
cout << "What is the first number you want to multiply?" << endl;
cin >> x ;
int y; 
cout << "What is the second number you wish to multiply?" << endl; 
cin >> y ;
int product; 
product = x * y;
cout << " The product is " << product << "." << endl;
break;
} 


default:
{
cout << "Try again";
break;
} 
}
return 0;
Oct 23, 2014 at 10:14am
Works fine. What is your input?
Oct 23, 2014 at 10:22am
Subtraction
Oct 23, 2014 at 10:24am
My output is always:

What is the number you would like to subtract from?
What is the number you would like to subtract?
The difference is -487472.

Regardless of what I type I get that.
Oct 23, 2014 at 10:25am
You mean, you write Substraction in console window? When you expect an int as input?
Oct 23, 2014 at 10:26am
Subtraction works.
Oct 23, 2014 at 10:39am
Ajh32 could you post your screen please? MiiNiPaa I type subtraction as the input because that's the point of the program. To type in an enum as the operation.
Oct 23, 2014 at 10:52am
Which operation would you like to use? 0
What is the number you would like to subtract from?
20
What is the number you would like to subtract?
8
The difference is 12
Oct 23, 2014 at 10:52am
Enum behaves as a glorified int.
1
2
3
4
5
enum Operation
{
OPERATION_SUBTRACTION,
OPERATION_ADDITION, OPERATION_DIVISION,
OPERATION_MULTIPLICATION[code]

};[/code]is similar to
1
2
3
4
const int OPERATION_SUBTRACTION = 0;
const int OPERATION_ADDITION = 1;
const int OPERATION_DIVISION = 2;
const int OPERATION_MULTIPLICATION = 3;

And additionally you have
1
2
int Operation = 1 ;
cin >> Operation; 
You declared operation as int. So you cannot enter anything but int in it.

You need to take your input as string. Then you either need to compare them with samples directly (replacing switch) or convert somehow to your Operation enum type.
Topic archived. No new replies allowed.