Guest User

Untitled

a guest
Dec 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Ch5Ex8
  4. //
  5. // Created by Bertoski on 10/01/12.
  6. // Copyright (c) 2012 Bertoski. All rights reserved.
  7. // This program is used as a math tutor v.2
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdlib>
  12. #include <ctime>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. int main()
  18. {
  19. int choice;//To hold menu choices
  20. float x = 10 + rand() % 1000;//Store x value
  21. float y = 1 + rand() % 1000;//Store y value
  22. float sum;// Store sum
  23. float difference;//Store difference
  24. float product;//Store product
  25. float quotient;//Store quotient
  26. float input; // Store input
  27.  
  28. // Setting srand time
  29. srand(time(0));
  30.  
  31.  
  32.  
  33.  
  34. //Constants for menu choices
  35. const int ADDITION_CHOICE = 1,
  36. SUBTRACTION_CHOICE = 2,
  37. MULTIPLICATION_CHOICE = 3,
  38. DIVISION_CHOICE = 4,
  39. QUIT_CHOICE = 5;
  40.  
  41. //Set the numeric output formatting.
  42. cout << fixed << showpoint << setprecision(2);
  43.  
  44. do
  45. {
  46.  
  47. //Display of Menu and Choices
  48. cout <<" \nMath tutoring problem generator\n\n"
  49. << "1. Addition \n"
  50. << "2. Subtraction \n"
  51. << "3. Multiplication \n"
  52. << "4. Division \n"
  53. << "5. Quit \n"
  54. << "Enter your choice (1-5): ";
  55. cin >> choice;
  56.  
  57.  
  58.  
  59. //Responce to user selection.
  60. while (choice < ADDITION_CHOICE || choice > QUIT_CHOICE)
  61. {
  62. cout <<"\nThe valid choices are 1 through 5. Run the \n"
  63. <<"program again and select one of the choices.\n";
  64. cin >> choice;
  65.  
  66.  
  67.  
  68. }
  69.  
  70. //Process the user's choice.
  71. if (choice != QUIT_CHOICE)
  72.  
  73.  
  74.  
  75.  
  76. //Respond to the user's menu selection.
  77. switch (choice)
  78. {
  79. case ADDITION_CHOICE:
  80. cout <<" " << x << endl; //Setting up random numbers
  81. cout <<" + " << y << endl; //Setting up random numbers
  82. cout << "_____________ \n";
  83. sum = (x + y);
  84. cin >> input;
  85.  
  86. if(input!= sum)
  87. {
  88. cout <<"\nSorry the correct answer is : "<< sum << endl;
  89. }
  90. else
  91. {
  92. //Correct display message
  93. cout <<"\nCongratulations!!! \n";
  94. cout <<"You have answered the problem correctly! \n";
  95. }
  96. break;
  97.  
  98.  
  99. case SUBTRACTION_CHOICE:
  100. cout <<" " << x << endl; //Setting up random numbers
  101. cout <<" - " << y << endl; //Setting up random numbers
  102. cout << "_____________ \n";
  103. difference = (x - y);
  104. cin >> input;
  105.  
  106. if(input!= difference)
  107. {
  108. cout <<"Sorry the correct answer is : "<< difference << endl;
  109. }
  110. else
  111. {
  112. //Correct display message
  113. cout <<"\nCongratulations!!! \n";
  114. cout <<"You have answered the problem correctly! \n";
  115. }
  116. break;
  117.  
  118. case MULTIPLICATION_CHOICE:
  119. cout << x; //Setting up random numbers
  120. cout <<" * " << y <<" = ? "<< endl; //Setting up random numbers
  121. product = (x * y);
  122. cin >> input;
  123.  
  124. if(input!= product)
  125. {
  126. cout <<"Sorry the correct answer is : "<< product << endl;
  127. }
  128. else
  129. {
  130. //Correct display message
  131. cout <<"\nCongratulations!!! \n";
  132. cout <<"You have answered the problem correctly! \n";
  133. }
  134. break;
  135.  
  136. case DIVISION_CHOICE:
  137. cout << x; //Setting up random numbers
  138. cout <<" / "<< y <<" = ? "<< endl; //Setting up random numbers
  139. quotient = (x / y);
  140. cin >> input;
  141.  
  142. if(input!= quotient)
  143. {
  144. cout <<"Sorry the correct answer is : "<< quotient << endl;
  145. }
  146. else
  147. {
  148. //Correct display message
  149. cout <<"\nCongratulations!!! \n";
  150. cout <<"You have answered the problem correctly! \n";
  151. }
  152. break;
  153.  
  154. case QUIT_CHOICE:
  155. cout <<"Program ending. \n";
  156.  
  157.  
  158. }
  159.  
  160.  
  161. } while (choice !=QUIT_CHOICE);
  162.  
  163. return 0;
  164. }
Add Comment
Please, Sign In to add comment