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
|
#include <iostream>
#include <limits>
using namespace std;
double getUserValue(double valueOne, double valueTwo, char calculationSymbol);
void displayResults(double valueOne, double valueTwo, char calculationSymbol, double answerValue);
main()
{
cout << " Welcome to my Calculator Program!" << endl;
cout << endl;
cout << "Enter any equation below, example: 3 * 7" << endl;
cout << "Operators include: * / + -" << endl;
cout << endl;
double valueOne = 0;
double valueTwo = 0;
char calculationSymbol;
double answerValue;
char response;
do{
getUserValue(valueOne,valueTwo,calculationSymbol);
displayResults(valueOne,valueTwo,calculationSymbol,answerValue);
cout << "Would you like to make another calculation? Y/N : ";
cin >> response;
cout << endl;
}while(toupper(response)=='Y');
cout << "\nThank you for using my Program!" << endl;
cout << endl;
return 0;
}
double getUserValue(double valueOne, double valueTwo, char calculationSymbol)
{
bool bFail;
do{
cout << "Equation: ";
cin >> valueOne >> calculationSymbol >> valueTwo;
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if(bFail == true){
cout << "ERROR: Please input valid Equation"<< endl;}
cout << endl;
}while(bFail == true);
}
void displayResults(double valueOne, double valueTwo, char calculationSymbol, double answerValue)
{
switch(calculationSymbol)
{case '+':
answerValue = valueOne + valueTwo;
break;
case '-':
answerValue = valueOne - valueTwo;
break;
case '*':
answerValue = valueOne * valueTwo;
break;
case '/':
answerValue = valueOne / valueTwo;
break;
}
cout<< "Answer: " << answerValue << endl;
}
|