I am having trouble figuring out how to store the operator that the user inputs. I think my line 36 is what is causing my problem but not sure so what happens (I'm sure you can see from the code but I can't ;)) is I get to line 35 and put in the operation to use for the integers and hit enter nothing happens I then type in any 2 alphanumeric characters hit enter and nothing then 2 alphanumeric and enter then it spits out my answer. I know it's probably something so easy I am missing.
Also after I get that part working I would like to add a "do while" loop for the user to continue to use whichever operator that was choose until iValue1 !=0
Lastly, is the simplest way to prevent a user to divide by 0 using an "if" cin.fail? If so would that go with my first "if" statement?
*Edit: Line 35 = "cout << "Enter the operation you want to perform:"
#include <iostream>
using namespace std;
int main()
{
float iValue1, iValue2, iValue3;
char chOperator1 = '/'; //Initializing operators
char chOperator2 = '*';
char chOperator3 = '+';
char chOperator4 = '-';
//Get user inputs
cout << "Enter the first value as an integer: ";
cin >> iValue1;
cout << "Enter the Second value as an integer: ";
cin >> iValue2;
cout << "Enter the operation you want to perform: ";
cin >> chOperator1 >> chOperator2 >> chOperator3 >> chOperator4;
if( chOperator1 == '/')
{
iValue3 = iValue1 / iValue2;
}
else {
if(chOperator2 == '*')
iValue3 = iValue1 * iValue2;
(chOperator3 == '+');
iValue3 = iValue1 + iValue2;
(chOperator4 == '-');
iValue3 = iValue1 - iValue2;
}
cout << "The result is \n " << iValue3;
return 0;
}
I suggest you use an array or a std::vector for multiple operations:
char operations[4];
cout >> "Enter 4 operations: ";
for (unsigned int i = 0; i < 4; ++i)
{
cin >> operation[i];
}
for (unsigned int j = 0; j < 4; ++j)
{
const char opr = operation[j];
switch (j)
{
case '*':
cout << (iValue1 * iValue2) << "\n";
break;
case '/':
cout << (iValue1 / iValue2) << "\n";
break;
case '+':
cout << (iValue1 + iValue2) << "\n";
break;
case '-':
cout << (iValue1 - iValue2) << "\n";
break;
default:
cout << "Invalid operation, '" << oper << "'\n";
break;
}
}