Advertisement
Guest User

ConsoleCalculator

a guest
Sep 26th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. void MENU(); // Function prototype for MENU function
  9. void Addition(); // Function prototype for Addition function
  10. void Subtraction(); // Function prototype for Addition function
  11. void RESTART(); // Function prototype for RESTART function
  12.  
  13. //Function prototypes used to keep the code clean and readable.
  14.  
  15. int main() //Main function
  16. {
  17.  
  18. MENU(); // MENU first to run in the main
  19.  
  20. return 0;
  21. }
  22.  
  23. void MENU() //When called upon the basic main menu should show up.
  24. {
  25. int answer; //answer variable used to navigate the menu
  26. int Addition(); //Function variable (i think) for Addition
  27. int Subtraction(); //Function variable possible for Subtraction
  28.  
  29. cout << " MENU\n"; //Text for the menu reading off the options the user has from 1 to 3
  30. cout << " 1.ADDITION\n";
  31. cout << "2.SUBTRACTION\n";
  32. cout << " 3.EXIT\n";
  33. cin >> answer; //User enters his answer
  34.  
  35. switch(answer){ //Switch runs through its options based on that answer variable
  36. case 1:// if case 1 is picked then it goes to the addition function
  37. Addition();
  38. break;
  39.  
  40. case 2:// if 2 is picked then it goes to the subtraction function
  41. Subtraction();
  42. break;
  43. case 3: // If the user enters 3 then it clears screen and ends the program.
  44. system("cls"); // a poor insecure way to clear the screen before exiting the program,
  45. //but currently the only way i know how to get it to work the way i desire it to.
  46. exit(0);
  47. break;
  48.  
  49. default :
  50. cout << "PLEASE INSIERT 1, 2 OR 3\n";//Prompting user to pick a valid answer
  51. MENU();//Redirecting user back the MENU function
  52. }
  53. }
  54.  
  55. void Addition() //When called upon the program should do a basic 2 number addition.
  56. {
  57. int fn;
  58. int sn;
  59. int sum;
  60.  
  61. cout << " ADDITON\n"; //Very simplistic almost DOS like menu for the Addition user interface
  62. cout << "ENTER YOUR FIRST NUMBER:\n"; //Prompting user for first number
  63. cin >> fn; // User enters first number
  64. cout << "\nENTER YOUR SECOND NUMBER:\n"; //Prompting user for first number
  65. cin >> sn; // User enters second
  66. sum = fn + sn; //Program adds the two together
  67. cout << "\nYOUR TOTAL IS: " << sum << endl; //Program prints out sum
  68. cout << endl;
  69. RESTART(); // Supposed to ask if the user wants to restart, instead goes straight to menu
  70. }
  71.  
  72. void Subtraction() //Everything here is supposed to act the same as addition
  73. //except its main purpose is to subtract the second number from the first number
  74. //but it fails the same way as the Addition function
  75. {
  76. int fn;
  77. int sn;
  78. int sum;
  79.  
  80. cout << " SUBTRACTION\n";
  81. cout << "ENTER YOUR FIRST NUMBER:\n";
  82. cin >> fn; //All functions here are the same as addition with the exception it subtracts values rather than adds them
  83. cout << "\nENTER YOUR SECOND NUMBER:\n";
  84. cin >> sn;
  85. sum = fn - sn;
  86. cout << "\nYOUR TOTAL IS: " << sum << endl;
  87. cout << endl;
  88. RESTART(); // same issue happens as soon as it's supposed to hit this bit of code
  89. }
  90.  
  91. void RESTART() //When called upon the program will restart or end the program, or so it should.
  92. {
  93. string input;
  94.  
  95. cout << "DO YOU WISH TO START OVER? Y/N"; //None of this makes an appearance in the code
  96. cin >> input;
  97. if (input == 'y')
  98. {
  99. MENU();
  100. }else if(input == 'n')
  101. {
  102. system("cls");// a poor insecure way to clear the screen before exiting the program
  103. //but currently the only way i know how to get it to work the way i desire it to.
  104. exit(0);
  105. }
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement