Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3. #include <fstream>
  4. #include <cstdlib>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. const int MAX_SIZE = 90;
  10. const int MAX_ARRY_SIZE = 1000;
  11.  
  12. struct MianMenu
  13. {
  14. int numberword;
  15. char wordslist [MAX_SIZE];
  16. };
  17.  
  18. struct QuizFormat
  19. {
  20. char question [MAX_SIZE];
  21. int choiseNumber;
  22. char choice [MAX_SIZE];
  23. int choiseNumber2;
  24. char choice2 [MAX_SIZE];
  25. int choiseNumber3;
  26. char choice3 [MAX_SIZE];
  27. int choiseNumber4;
  28. char choice4 [MAX_SIZE];
  29. int answer;
  30. };
  31.  
  32. void displayQuiz(QuizFormat [], int );
  33. bool readQuizFromFile(string &fileName, QuizFormat quiz[], int &numQuestion);
  34. void displayMenu(MianMenu [], int );
  35. bool readMenuFromFile(string &fileName, MianMenu menu[], int &numMenu);
  36.  
  37.  
  38. int main()
  39. {
  40. QuizFormat myQuiz[MAX_ARRY_SIZE];
  41. MianMenu myMenu[MAX_ARRY_SIZE];
  42.  
  43. int userMenupick;
  44. string userName;
  45. string fileName = "main-menu.txt";
  46. int numberOfMenu;
  47.  
  48. cout << "What is your name?" << endl;
  49. getline(cin,userName);
  50.  
  51. cout << "Please choose a quiz to do from the list below." << endl;
  52.  
  53. if (!readMenuFromFile(fileName, myMenu, numberOfMenu))
  54. {
  55. cout << "Trouble locating the file. Exiting....." << endl;
  56. }
  57. else
  58. {
  59. displayMenu(myMenu, numberOfMenu);
  60. }
  61.  
  62.  
  63. do
  64. {
  65. cin >> userMenupick;
  66.  
  67. if(cin.fail())
  68. {
  69. cin.clear();
  70. cout << "Only numbers." << endl;
  71. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  72. }
  73.  
  74. switch(userMenupick)
  75. {
  76. case 1:
  77. {
  78. cout << "US State Capitals." << endl;
  79.  
  80. string fileName = "us-state-capitals.txt";
  81. int numberOfQuiz;
  82.  
  83. if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
  84. {
  85. cout << "Trouble locating the file. Exiting....." << endl;
  86. }
  87. else
  88. {
  89. displayQuiz(myQuiz, numberOfQuiz);
  90. }
  91.  
  92. return 0;
  93. break;
  94. }
  95. case 2:
  96. {
  97. cout << "World Capital Cities." << endl;
  98.  
  99. string fileName = "world-state-capitals.txt";
  100. int numberOfQuiz;
  101.  
  102. if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
  103. {
  104. cout << "Trouble locating the file. Exiting....." << endl;
  105. }
  106. else
  107. {
  108. displayQuiz(myQuiz, numberOfQuiz);
  109. }
  110.  
  111. return 0;
  112.  
  113. break;
  114. }
  115. case 3:
  116. {
  117. cout << "Video Games" << endl;
  118.  
  119. string fileName = "video-games.txt";
  120. int numberOfQuiz;
  121.  
  122. if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
  123. {
  124. cout << "Trouble locating the file. Exiting....." << endl;
  125. }
  126. else
  127. {
  128. displayQuiz(myQuiz, numberOfQuiz);
  129. }
  130.  
  131. return 0;
  132.  
  133.  
  134. break;
  135. }
  136. case 4:
  137. {
  138. cout << "C++." << endl;
  139.  
  140. string fileName = "cpp.txt";
  141. int numberOfQuiz;
  142.  
  143. if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
  144. {
  145. cout << "Trouble locating the file. Exiting....." << endl;
  146. }
  147. else
  148. {
  149. displayQuiz(myQuiz, numberOfQuiz);
  150. }
  151.  
  152. return 0;
  153.  
  154.  
  155. break;
  156. }
  157. case 5:
  158. {
  159. cout << "History." << endl;
  160.  
  161. string fileName = "history.txt";
  162. int numberOfQuiz;
  163.  
  164. if (!readQuizFromFile(fileName, myQuiz, numberOfQuiz))
  165. {
  166. cout << "Trouble locating the file. Exiting....." << endl;
  167. }
  168. else
  169. {
  170. displayQuiz(myQuiz, numberOfQuiz);
  171. }
  172.  
  173. return 0;
  174.  
  175. break;
  176. }
  177. case 6:
  178. {
  179. cout << "Exit" << endl;
  180. return 0;
  181. }
  182.  
  183. break;
  184. default:
  185. cout << "Pick only 1,2,3,4,5,or 6." << endl;
  186. break;
  187. }
  188.  
  189. }while (userMenupick > 1 || userMenupick < 6);
  190.  
  191.  
  192.  
  193. return 0;
  194. }
  195. void displayQuiz(QuizFormat quiz[], int numQuestion)
  196. {
  197. int userawsure;
  198. double correctawsure = 0;
  199. double wrongawsure = 0;
  200. double avg;
  201.  
  202. for (int i=0; i<numQuestion; i++)
  203. {
  204.  
  205. if(cin.fail())
  206. {
  207. cin.clear();
  208. cout << "Only numbers." << endl;
  209. cout << "Pick only 1,2,3,4,5,or 6" << endl;
  210. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  211. }
  212.  
  213. cout << quiz[i].question << endl;
  214. cout << quiz[i].choiseNumber << "." << quiz[i].choice << endl;
  215. cout << quiz[i].choiseNumber2 << "." << quiz[i].choice2 << endl;
  216. cout << quiz[i].choiseNumber3 << "." << quiz[i].choice3 << endl;
  217. cout << quiz[i].choiseNumber4 << "." << quiz[i].choice4 << endl;
  218. cout << "Your choice:";
  219. cin >> userawsure;
  220.  
  221. if(userawsure == quiz[i].answer)
  222. {
  223. cout << "correct" << endl;
  224. correctawsure++;
  225. }
  226. else if(userawsure == quiz[i].answer)
  227. {
  228. cout << "wrong" << endl;
  229. wrongawsure++;
  230. }
  231.  
  232.  
  233. cout << "You got " << correctawsure << "right." << endl;
  234. cout << "You got " <<wrongawsure << "wrong." << endl;
  235.  
  236. if(correctawsure < wrongawsure)
  237. {
  238. avg = correctawsure/wrongawsure;
  239.  
  240. avg = avg * 100.0;
  241.  
  242. cout << "Your Avg " << avg << endl;
  243. }
  244. else
  245. {
  246. avg = wrongawsure/correctawsure;
  247.  
  248. avg = avg * 100.0;
  249.  
  250. cout << "Your Avg " << avg << endl;
  251. }
  252. }
  253. }
  254.  
  255.  
  256. bool readQuizFromFile(string &fileName, QuizFormat quiz[], int &numQuestion)
  257. {
  258. ifstream iFile;
  259. bool retVal = true;
  260. int i;
  261. char temp[MAX_SIZE];
  262.  
  263.  
  264. iFile.open(fileName.c_str());
  265. if (!iFile)
  266. retVal = false;
  267. else
  268. {
  269. i=0;
  270. while (!iFile.eof())
  271. {
  272. iFile.getline(quiz[i].question, MAX_SIZE, ',');
  273.  
  274. iFile.getline(temp, MAX_SIZE, ',');
  275.  
  276. quiz[i].choiseNumber = atoi (temp);
  277.  
  278. iFile.getline(quiz[i].choice, MAX_SIZE, ',');
  279.  
  280. iFile.getline(temp, MAX_SIZE, ',');
  281.  
  282. quiz[i].choiseNumber2 = atoi (temp);
  283.  
  284. iFile.getline(quiz[i].choice2, MAX_SIZE, ',');
  285.  
  286. iFile.getline(temp, MAX_SIZE, ',');
  287.  
  288. quiz[i].choiseNumber3 = atoi (temp);
  289.  
  290. iFile.getline(quiz[i].choice3, MAX_SIZE, ',');
  291.  
  292. iFile.getline(temp, MAX_SIZE, ',');
  293.  
  294. quiz[i].choiseNumber4 = atoi (temp);
  295.  
  296. iFile.getline(quiz[i].choice4, MAX_SIZE, ',');
  297.  
  298. iFile.getline(temp, MAX_SIZE, ',');
  299.  
  300. quiz[i].answer = atof (temp);
  301.  
  302.  
  303.  
  304. char nextChar;
  305. nextChar = iFile.peek();
  306.  
  307. if (nextChar == EOF)
  308. {
  309. numQuestion = i;
  310. break;
  311. }
  312.  
  313. i++;
  314. }
  315. }
  316.  
  317. iFile.close();
  318.  
  319. return retVal;
  320. }
  321.  
  322. void displayMenu(MianMenu menu[], int numMenu)
  323. {
  324. for (int i=0; i<numMenu; i++)
  325. {
  326. cout << menu[i].numberword << "." << menu[i].wordslist << endl;
  327. }
  328. }
  329.  
  330.  
  331. bool readMenuFromFile(string &fileName, MianMenu menu[], int &numMenu)
  332. {
  333. ifstream iFile;
  334. bool retVal = true;
  335. int i;
  336. char temp[MAX_SIZE];
  337.  
  338.  
  339. iFile.open(fileName.c_str());
  340. if (!iFile)
  341. retVal = false;
  342. else
  343. {
  344. i=0;
  345. while (!iFile.eof())
  346. {
  347. iFile.getline(temp, MAX_SIZE, ',');
  348.  
  349. menu[i].numberword = atoi (temp);
  350.  
  351. iFile.getline(menu[i].wordslist, MAX_SIZE, ',');
  352.  
  353. char nextChar;
  354. nextChar = iFile.peek();
  355.  
  356. if (nextChar == EOF)
  357. {
  358. numMenu = i;
  359. break;
  360. }
  361.  
  362. i++;
  363. }
  364. }
  365.  
  366. iFile.close();
  367.  
  368. return retVal;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement