Advertisement
Guest User

C++ Calc

a guest
Feb 10th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void start()
  5. {
  6. using namespace std;
  7.  
  8. cout << "Welcome to my Basic Mini-Calculator!" << endl;
  9. }
  10.  
  11. int choice()
  12. {
  13. using namespace std;
  14.  
  15. int uChoice;
  16.  
  17.  
  18. cout << endl << "What do you want to do?" << endl;
  19. cout << "1) Add" << endl;
  20. cout << "2) Subtract" << endl;
  21. cout << "3) Multiply" << endl;
  22. cout << "4) Divide" << endl;
  23. cout << endl << "Waiting for input... (enter a number): ";
  24. cin >> uChoice;
  25. cout << endl;
  26.  
  27. while( uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4 );
  28.  
  29. switch ( uChoice )
  30. {
  31. case 1:
  32. cout << endl << "You chose addition." << endl;
  33. break;
  34.  
  35. case 2:
  36. cout << endl << "You chose subtraction." << endl;
  37. break;
  38.  
  39. case 3:
  40. cout << endl << "You chose multiplication." << endl;
  41. break;
  42.  
  43. case 4:
  44. cout << endl << "You chose division." << endl;
  45. break;
  46. }
  47.  
  48. return uChoice;
  49.  
  50.  
  51.  
  52. }
  53.  
  54. int input( bool i = false )
  55. {
  56. using namespace std;
  57.  
  58. string text;
  59.  
  60. text = ( i == true ) ? "Enter another number: " : "Enter a number: ";
  61. cout << endl << text;
  62.  
  63. float number;
  64. cin >> number;
  65.  
  66. return number;
  67. }
  68.  
  69. int work( int one, int two, int todo )
  70. {
  71. using namespace std;
  72.  
  73. float answer;
  74.  
  75. switch ( todo )
  76. {
  77. case 1:
  78. answer = one + two;
  79. break;
  80.  
  81. case 2:
  82. answer = one - two;
  83. break;
  84.  
  85. case 3:
  86. answer = one * two;
  87. break;
  88.  
  89. case 4:
  90. answer = one / two;
  91. break;
  92.  
  93. default: cout << "Please choose a proper number (1-4)" << endl;
  94. }
  95.  
  96.  
  97. return answer;
  98. }
  99.  
  100. void answer( int theanswer )
  101. {
  102. using namespace std;
  103.  
  104. cout << endl << "The answer is " << theanswer << "." << endl;
  105. cout << endl << "Hit Return to exit.";
  106.  
  107. cin.clear();
  108. cin.ignore( 255, '\n' );
  109. cin.get();
  110. }
  111.  
  112. int main()
  113. {
  114. using namespace std;
  115.  
  116. start();
  117.  
  118. int todo = choice();
  119.  
  120. float one = input();
  121. float two = input( true );
  122.  
  123. float theanswer = work( one, two, todo );
  124.  
  125.  
  126. answer( theanswer );
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement