Handling a two character operator in a switch statement
Hi!
I am setting up a program where the user enters two fractions and an operator (arithmetic or relational) and the program performs the operation and displays the result. I am having trouble figuring out how to handle the two character relational operators. Does anyone have any ideas.
I tried:
char oper1, oper2;
cin >> oper1 >> oper2;
but the program hangs when there isn't a second operator.
Then I tried:
string oper;
getline(cin, oper);
but my switch statement doesn't like that because oper is now a string.
I was thinking about creating an enum with all the operators and using that in an if else, then using the eum type in my switch, but there has to be a better, shorter way. But, I am stumped.
This is the code I have:
Code:
#include <string>
using namespace std;
#include "fractionType.h"
int main()
{
fractionType x;
fractionType y;
fractionType z;
string operStr;
cout << "\nThis program performs aritnmetic and relational operations"
<< "\non two fractions that you enter.";
const string MENU = "\nThe operations that you can use are:"
"\n Addition -------------------> use '+'"
"\n Subtraction ----------------> use '-'"
"\n Multiplication -------------> use '*'"
"\n Division -------------------> use '/'"
"\n Equal To -------------------> use '=='"
"\n Not Equal To ---------------> use '!='"
"\n Less Than ------------------> use '<'"
"\n Greater Than ---------------> use '>'"
"\n Less Than or Equal To ------> use '<='"
"\n Greater Than or Equal To ---> use '>='"
"\n Or to QUIT -----------------> enter 'Q'"
"\nEnter the required symbol when prompted."
"\nEnter the first fraction in the form a/b: ";
cout << MENU;
cin >> x;
cout << "\nEnter the second fraction in the form a/b: ";
cin >> y;
cout << "\nEnter the operation symbol: ";
getline(cin, operStr);
cin.ignore();
while (oper != "Q" || oper != "q")
{
if
cout << endl;
switch(oper)
{
case '==':
{
if (x == y)
cout << x << " is Equal to " << y << endl;
else
cout << x << " is Not Equal to " << y << endl;
break;
}//end case '=='
case '!=':
{
if (x != y)
cout << x << " is Not Equal to " << y << endl;
else
cout << x << " is Equal to " << y << endl;
break;
}//end case '!='
case '<=':
{
if (x <= y)
cout << x << " is Less Than or Equal to " << y << endl;
else
cout << x << " is Not Less Than or Equal to " << y << endl;
break;
}//end case '<='
case '>=':
{
if (x >= y)
cout << x << " is Greater Than or Equal to " << y << endl;
else
cout << x << " is Not Greater Than or Equal to " << y << endl;
break;
}//end case '>='
case '+':
{
z = x + y;
cout << "The sum of " << x << " + " << y << " = " << z << endl;
break;
}//end case '+'
case '-':
{
z = x - y;
cout << "The difference of " << x << " - " << y << " = " << z << endl;
break;
}//end case '-'
case '*':
{
z = x * y;
cout << "The product of " << x << " * " << y << " = " << z << endl;
break;
}//end case '*'
case '/':
{
z = x / y;
cout << "The result of " << x << " / " << y << " = " << z << endl;
break;
}//end case '/'
default:
cerr << "\nInvalid operation choice, please try again!";
}//end switch
cout << MENU;
cin >> x;
cout << "\nEnter the second fraction in the form a/b: ";
cin >> y;
cout << "\nEnter the operation symbol: ";
getline(cin, oper);
cin.ignore();
}//end while
}//end main
Thanks for any input.