Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <ctype.h> // this also has in it to upper and tolower used below
- #include <stdlib.h> // include stdlib.h if you are using exit in the code below
- void main ()
- {
- char Answer;
- int Num1;
- char Op;
- bool ValidOp;
- do {
- cout << "=> ";
- cin >> Num1; // enter first number
- cout << "=> ";
- do {
- cin >> Op; // enter an operator
- switch (Op) // check if operator is valid
- {
- case 'X':
- case 'x': // turn calculator off
- cout << "Are you sure? ";
- cin >> Answer;
- if (toupper (Answer) == 'Y')
- {
- cout << "Bye" << endl;
- exit (0); // terminate the program immediately
- }
- else
- {
- ValidOp = false;
- cout << "Another Operator => ";
- break;
- }
- case 'C':
- case 'c': // clear (display 0)
- cout << "=> 0" << endl;
- case '+':
- case '-':
- case '*':
- case '/':
- ValidOp = true;
- break;
- default:
- ValidOp = false;
- cout << "Invalid, try again: => ";
- }
- } while (!ValidOp); // if not valid go back and read another operator
- } while ();
- // go back and read a new first number
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement