Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <time.h>
  6. #include <string>
  7. using namespace std;
  8.  
  9.  
  10. //Global for file output
  11. const string ADD = "Addition";
  12. const string SUB = "Subtraction";
  13. const string MULTIPLY = "Multiplication";
  14.  
  15.  
  16. //function prototypes
  17. void menu(int &);
  18. void saveFile(ofstream &, int, string);
  19.  
  20. //main function for operators
  21. int main()
  22. {
  23. //Variables for menu
  24. int selection = 0;
  25. const int QUIT = -1;
  26.  
  27. //Output file
  28. ofstream worksheet;
  29. worksheet.open("worksheet.txt");
  30.  
  31.  
  32. //file failures
  33. if (worksheet.fail())
  34. {
  35. cout << "CANNOT OPEN WORKSHEET.TXT, EXITING.../n/n";
  36. system("pause");
  37. exit(1);
  38. }
  39.  
  40. //actual output file contents
  41. worksheet << "Operation:"
  42. << setw(20)<< "Attempts:";
  43.  
  44. //variables for numbers, attempts, input for user, operator
  45. const int MAX_NUM = 999;
  46. int num1, num2, answer, userAnswer, attempts;
  47. userAnswer = 0;
  48.  
  49. //variable for operator
  50. string operate;
  51.  
  52. //Seed randomizer for operations
  53. srand(unsigned int(time(NULL)));
  54.  
  55.  
  56.  
  57.  
  58. while (selection != QUIT)
  59. {
  60. //Run menu function, get selection
  61. menu(selection);
  62.  
  63. //Addition
  64. if (selection == 1)
  65. {
  66. //If this is selected, sets string to add to
  67. //put addition in txt file, rolls numbers to
  68. //be added together
  69.  
  70. operate = ADD;
  71. attempts = 1;
  72. num1 = (rand() % MAX_NUM) + 1;
  73. num2 = (rand() % MAX_NUM) + 1;
  74. answer = num1 + num2;
  75. cout << endl << endl << setw(35) << num1 << endl;
  76. cout << setw(30) << "+" << setw(5) << num2 << endl;
  77. cout << setw(35) << "-----------" << endl;
  78. cout << "Please enter your answer: ";
  79. cin >> userAnswer;
  80.  
  81. //If user gets the answer wrong, prompt for reenetry
  82.  
  83. while (userAnswer != answer)
  84. {
  85. cout << endl << "Sorry, try again!!" << endl;
  86. attempts++;
  87. cout << "Current attempt: " << attempts << endl;
  88. cout << "Please enter your answer: ";
  89. cin >> userAnswer;
  90. }
  91.  
  92. //Once answer is correct, call file out function
  93. //to record operation and how many attempts it
  94. //took to get the correct answer
  95.  
  96. cout << "Congratualtions!!!" << endl;
  97. cout << "Returning to menu..." << endl;
  98. saveFile(worksheet, attempts, operate);
  99. system("pause");
  100. system("cls");
  101.  
  102. }//Addition
  103.  
  104.  
  105. //Subtraction
  106. else if (selection == 2)
  107. {
  108.  
  109. //If this is selected, sets string to SUB to
  110. //put subtraction in txt file, rolls numbers to
  111. //be subtracted
  112.  
  113. operate = SUB;
  114. attempts = 1;
  115. num1 = (rand() % MAX_NUM) + 1;
  116. num2 = (rand() % MAX_NUM) + 1;
  117.  
  118. //If num2>num1, re-roll numbers to ensure
  119. //no negative answers, keeps it simple
  120.  
  121. while (num2 > num1)
  122. {
  123. num1 = (rand() % MAX_NUM) + 1;
  124. num2 = (rand() % MAX_NUM) + 1;
  125. }
  126.  
  127. //Sets answer, displays the numbers and operator
  128. //prompts for answer
  129.  
  130. answer = num1 - num2;
  131. cout << endl << endl << setw(35) << num1 << endl;
  132. cout << setw(30) << "-" << setw(5) << num2 << endl;
  133. cout << setw(35) << "-----------" << endl;
  134. cout << "Please enter your answer: ";
  135. cin >> userAnswer;
  136.  
  137. //If user gets the answer wrong, prompt for reenetry
  138.  
  139. while (userAnswer != answer)
  140. {
  141. cout << endl << "Sorry, try again!!" << endl;
  142. attempts++;
  143. cout << "Current attempt: " << attempts << endl;
  144. cout << "Please enter your answer: ";
  145. cin >> userAnswer;
  146. }
  147.  
  148. //Once answer is correct, call file out function
  149. //to record operation and how many attempts it
  150. //took to get the correct answer
  151.  
  152. cout << "Congratualtions!!!" << endl;
  153. cout << "Returning to menu..." << endl;
  154. saveFile(worksheet, attempts, operate);
  155. system("pause");
  156. system("cls");
  157.  
  158. }//Subtraction end
  159.  
  160.  
  161. //Multiplication
  162. else if (selection == 3)
  163. {
  164.  
  165. //If this is selected, sets the string
  166. //to MULTIPLY to put Multiplication in
  167. //txt file, rolls number to be multiplied
  168.  
  169. operate = MULTIPLY;
  170. attempts = 1;
  171. num1 = (rand() % MAX_NUM) + 1;
  172. num2 = (rand() % MAX_NUM) + 1;
  173.  
  174. //Sets answer, displays output of numbers,
  175. //prompts for answer
  176.  
  177. answer = num1 * num2;
  178. cout << endl << endl << setw(35) << num1 << endl;
  179. cout << setw(30) << "*" << setw(5) << num2 << endl;
  180. cout << setw(35) << "-----------" << endl;
  181. cout << "Please enter your answer: ";
  182. cin >> userAnswer;
  183.  
  184. //If user gets the answer wrong, prompt for reenetry
  185.  
  186. while (userAnswer != answer)
  187. {
  188. cout << endl << "Sorry, try again!!" << endl;
  189. attempts++;
  190. cout << "Current attempt: " << attempts << endl;
  191. cout << "Please enter your answer: ";
  192. cin >> userAnswer;
  193. }
  194.  
  195. //Once answer is correct, call file out function
  196. //to record operation and how many attempts it
  197. //took to get the correct answer
  198.  
  199. cout << "Congratualtions!!!" << endl;
  200. cout << "Returning to menu..." << endl;
  201. saveFile(worksheet, attempts, operate);
  202. system("pause");
  203. system("cls");
  204.  
  205.  
  206. }//Multiplication
  207.  
  208. }
  209. cout << "You can view the operators and attempts at worksheet.txt" << endl;
  210. system("pause");
  211. return 0;
  212. }//main
  213.  
  214. //Menu
  215. void menu(int &selction)
  216. {
  217. cout << "Welcome to the math work along program!" << endl;
  218. cout << "This will generate 2 numbers with the selected operator and\n allow you to solve the equation" << endl;
  219. cout << "1. Addition\n2. Subtraction\n3. Multiplication\nOr type -1 to quit\n";
  220. cin >> selection;
  221. while ((selection > 3) && (selection < 1) && (selection != -1))
  222. {
  223. cout << "Please enter a valid choice: ";
  224. cin >> selection;
  225. }
  226.  
  227. }//menu
  228.  
  229. //File write function
  230. //Outputs
  231. void saveFile(ofstream &worksheet, int attempts, string operate)
  232. {
  233. worksheet << endl << operate << setw(20) << attempts;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement