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.
#include <iostream>
usingnamespace 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;
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.
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
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.