Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10. int choice, problems, problemsDone, i, num1, num2, correct;
  11. float userAnswer, actualAnswer, grade;
  12. double dividedAnswer;
  13. string name;
  14. char ch;
  15. void menu();
  16.  
  17. int main()
  18. {
  19. cout << "Math Tutor \n";
  20. cout << "\n";
  21. cout << "What is your name? \n";
  22. getline(cin, name);
  23. cout << "Welcome, " << name << "!\n";
  24.  
  25. menu();
  26.  
  27. cin.get(ch);
  28. ch = cin.get();
  29.  
  30. }
  31. void menu()
  32. {
  33. cout << "\n";
  34. cout << "Please choose one of the following options! \n";
  35. cout << "1. Addition \n";
  36. cout << "2. Subtraction \n";
  37. cout << "3. Multiplication \n";
  38. cout << "4. Division \n";
  39. cout << "5. Quit \n";
  40. while (true)
  41. {
  42. if (!(cin >> choice))
  43. {
  44. cout << "Input must be an integer. Please try again. \n";
  45. cin.clear();
  46. cin.ignore();
  47. menu();
  48. }
  49. switch (choice)
  50. {
  51. case 1: //addition
  52. cout << "How many problems would you like? \n";
  53. cin >> problems;
  54. if (problems > 1)
  55. {
  56. do
  57. {
  58. srand(time(0));
  59. num1 = rand() % 100;
  60. num2 = rand() % 100;
  61. cout << "What is " << num1 << "+" << num2 << "?\n";
  62. cin >> userAnswer;
  63. actualAnswer = num1 + num2;
  64. if (userAnswer == actualAnswer)
  65. {
  66. cout << "Good job! \n";
  67. ++correct;
  68. }
  69. else {
  70. cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
  71. }
  72. ++problemsDone;
  73. } while (problemsDone < problems);
  74. grade = (correct * 100) / problems;
  75. cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
  76. break;
  77. }
  78. else
  79. {
  80. cout << "Invalid entry. \n";
  81. menu();
  82. }
  83. break;
  84.  
  85.  
  86. case 2: //subtraction
  87. cout << "How many problems would you like? \n";
  88. cin >> problems;
  89. if (problems > 1)
  90. {
  91. do
  92. {
  93. srand(time(0));
  94. num1 = rand() % 100;
  95. num2 = rand() % 100;
  96. cout << "What is " << num1 << "-" << num2 << "?\n";
  97. cin >> userAnswer;
  98. actualAnswer = num1 - num2;
  99. if (userAnswer == actualAnswer)
  100. {
  101. cout << "Good job! \n";
  102. ++correct;
  103. }
  104. else {
  105. cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
  106. }
  107. ++problemsDone;
  108. } while (problemsDone < problems);
  109. grade = (correct * 100) / problems;
  110. cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
  111. }
  112. else {
  113. cout << "Invalid entry. \n";
  114. menu();
  115. }
  116. break;
  117.  
  118. case 3: //multiplication
  119. cout << "How many problems would you like? \n";
  120. cin >> problems;
  121. if (problems > 1)
  122. {
  123. do
  124. {
  125. srand(time(0));
  126. num1 = rand() % 100;
  127. num2 = rand() % 100;
  128. cout << "What is " << num1 << "*" << num2 << "?\n";
  129. cin >> userAnswer;
  130. actualAnswer = num1 * num2;
  131. if (userAnswer == actualAnswer)
  132. {
  133. cout << "Good job! \n";
  134. ++correct;
  135. }
  136. else {
  137. cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
  138. }
  139. ++problemsDone;
  140. } while (problemsDone < problems);
  141. grade = (correct * 100) / problems;
  142. cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
  143. break;
  144. }
  145. else
  146. {
  147. cout << "Invalid entry. \n";
  148. menu();
  149. }
  150. break;
  151.  
  152. case 4: //division
  153. cout << "How many problems would you like? \n";
  154. cin >> problems;
  155. if (problems > 1)
  156. {
  157. do
  158. {
  159. srand(time(0));
  160. num1 = rand() % 100 + 1;
  161. num2 = rand() % 100 + 1;
  162. cout << "What is " << num1 << "/" << num2 << "?\n";
  163. cin >> userAnswer;
  164. dividedAnswer = (double)num1 / num2;
  165. if (userAnswer == dividedAnswer)
  166. {
  167. cout << "Good job! \n";
  168. ++correct;
  169. }
  170. else {
  171. cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
  172. }
  173. ++problemsDone;
  174. } while (problemsDone < problems);
  175. grade = (correct * 100) / problems;
  176. cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
  177. break;
  178. }
  179. else {
  180. cout << "Invalid entry.\n";
  181. menu();
  182. }
  183. break;
  184.  
  185. case 5: //quit
  186. cout << "Goodbye!" << endl;
  187. return;
  188.  
  189. default: //handler
  190. cout << "Incorrect, please try again. \n \n";
  191. menu();
  192. break;
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement