...

View Full Version : Calculator Problem



va396053
09-24-2012, 01:25 AM
Assignment:
Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator

This is what I did. I am not sure whether it is right or not. Can anyone help me with this?
Thanks

#include <iostream>

using namespace std;

#include <stdlib.h>

void main()
{
bool InvalidOp;
int Number1;
int Number2;
char op;

cout << "Please enter a number and press enter: ";
cin >> Number1;

cout << "Please enter another number and press enter: ";
cin >> Number2;

cout << "Please enter a a valid operator, 'c' to clear, or 'x' to turn off and press ENTER : ";
cin >> op;
do {
cout << "Enter an operator [+-*/ccXx]: ";
cin >> Op;
InvalidOp = false;
switch (op)
{
case '+':
cout << "The result is: " << Number1 + Number2 << endl;
break;

case '-':
cout << "The result is: " << Number1 - Number2 << endl;
break;

case '*':
cout << "The result is: " << Number1 * Number2 << endl;
break;

case '/':
cout << "The result is: " << Number1 / Number2 << endl;
break;

case 'C':
case 'c':
break;

case 'X':
case 'x':
cout << "Bye" << endl;
exit (0);

default:
cout << "Invalid operator" << endl;
InvalidOp = true;
}
} while (InvalidOp);

}

sunfighter
09-25-2012, 01:41 AM
http://www.anaesthetist.com/mnm/javascript/calc.htm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum