the program is run, it initiliazes the result to 0. The user can then type in an operator
and number. The result is updated and displayed. The following operators are valid:
Operator Meaning
--------------------------------
+ Addition
- Subtraction
* Multiplication
/ Division
---------------------------------
Handle the case when the user enters q or Q to quit and h or H for usage help.
Problem: when the program runs and the user enter an operator, it goes on and on and on.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int math;
int number;
int result = 0;
char H,h, Q, q;
cout << " Result: " << result;
cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
cin >> math;
cout << endl;
cout << "Enter the value: ";
cin >> number;
cout << endl;
while (math != Q || math != q) {
cout << endl;
cout << setw(10) << "Operator" << setw(15) << "Meaning" << endl;
cout << setw(10) << " ----------------------------" << endl;
cout << setw(5) << "+" << setw(20) << "Addition" << endl;
cout << setw(5) << "-" << setw(20) << "Subtraction" << endl;
cout << setw(5) << "*" << setw(20) << "Multiplication" << endl;
cout << setw(5) << "/" << setw(20) << "Division" << endl;
cout << "Enter the value: ";
cin >> number;
cout << endl;
cout << endl;
cout << "Enter operator from the table below, Q or q for quit, H or h for help:" << endl;
cin >> math;
cout << endl;
cout << " Result: " << result;
cout << endl;
cout << endl;
switch (math) {
case '+':
result += number;
break;
case '-':
result -= number;
break;
case '*':
result *= number;
break;
case '/':
result /= number;
if (number==0);
cout << "Division by zero";
cout << endl;
break;
case 'H':
case 'h':
cout << "Use '+' to add a value to the result" << endl;
cout << "Use '-' to subtract a value from the result" << endl;
cout << "Use '*' to multiply a value with the result" << endl;
cout << "Use '/' to divide a value into the result" << endl;
break;
case 'Q':
case 'q':
cout << "Thank you for using our calculator. Come back again :-)." << endl;
break;
system ("pause");
}
}
system ("pause");
return 0;
}

New Topic/Question
Reply




MultiQuote







|