Advertisement
Guest User

halp

a guest
Sep 18th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <ctype.h> // this also has in it to upper and tolower used below
  6. #include <stdlib.h> // include stdlib.h if you are using exit in the code below
  7.  
  8. void main ()
  9. {
  10. char Answer;
  11. int Num1;
  12. char Op;
  13. bool ValidOp;
  14.  
  15. do {
  16. cout << "=> ";
  17. cin >> Num1; // enter first number
  18. cout << "=> ";
  19. do {
  20. cin >> Op; // enter an operator
  21. switch (Op) // check if operator is valid
  22. {
  23. case 'X':
  24. case 'x': // turn calculator off
  25. cout << "Are you sure? ";
  26. cin >> Answer;
  27. if (toupper (Answer) == 'Y')
  28. {
  29. cout << "Bye" << endl;
  30. exit (0); // terminate the program immediately
  31. }
  32. else
  33. {
  34. ValidOp = false;
  35. cout << "Another Operator => ";
  36. break;
  37. }
  38. case 'C':
  39. case 'c': // clear (display 0)
  40. cout << "=> 0" << endl;
  41. case '+':
  42. case '-':
  43. case '*':
  44. case '/':
  45. ValidOp = true;
  46. break;
  47. default:
  48. ValidOp = false;
  49. cout << "Invalid, try again: => ";
  50. }
  51. } while (!ValidOp); // if not valid go back and read another operator
  52. } while ();
  53.  
  54. // go back and read a new first number
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement