candyapplecorn

TriviaQuestionGame.cpp

Nov 20th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.57 KB | None | 0 0
  1. ///..............................................................................................
  2. ///
  3. ///TRIVIA GAME
  4. ///aka: "This Must Be The 6th Time I Have Re-Written This File"
  5. ///Homework #6
  6. ///
  7. ///This is a trivia game program. It uses a class called Question.h! This program used to simlate
  8. ///a trivia game. In this program, the contestants' answers were random. However in a real trivia
  9. ///game, contestants don't answer randomly! But I thought the requirements for this assignment didn't say
  10. ///to make a real trivia game. So I didn't. But then I re-read the requirements a berjillion times and
  11. ///realized the book was asking for a real trivia game that would take answers from input, aka "cin".
  12. ///So I remade it AGAIN. Now it's a REAL trivia game, not a fake simulated one. Boy howdy!
  13. ///
  14. ///The file TriviaQuestions.txt can be as many questions as you want! It just has to have the number
  15. ///of questions at the top. It would be possible to make an input file without the number of
  16. ///questions at the top, but that would be more difficult, and previous input files have
  17. ///had a number at the top indicating how long a list is, or something similar. So I figured it'd
  18. ///be okay.
  19. ///
  20. ///Joseph Burger
  21. ///
  22. #include <iostream>
  23. #include <cstdlib>
  24. #include <string>
  25. #include <fstream>
  26. #include <iomanip>
  27. #include <ctime>
  28. #include "Question.h"
  29. using namespace std;
  30.  
  31. void getNamesAndNumQuestions(ifstream &trQs, string *name, int &numQs);
  32. void destroy(Question **qPtr, int numQs);
  33. void printTrivia(Question **qPtr, int numQs, int *score, string *name, int NUMBEROFQUESTIONSTOASK);
  34. void randomize(Question **qPtr, int numQs);
  35. void showResults(Question **qPtr, string *name);
  36. void pressSpaceToUnpause();
  37. //..............................................................................................
  38. //main is responsible for seeding the random number generator, creating dynamically allocated
  39. //question objects, and calling functions which store questions from question.txt into Question
  40. //objects, and calls member functions from the Question class and functions from this source
  41. //file. It throws everything together. It's main().
  42. //..............................................................................................
  43. int main()
  44. {
  45.     const int NUMBEROFQUESTIONSTOASK = 10; //Two contestants will answer 5 questions each. The whole process of asking contestants will be controlled with a for loop which doesn't exceed 10 iterations
  46.     srand(time(NULL));
  47.     ifstream trQs;
  48.     string name[2];
  49.     int numQs, score[2];
  50.     getNamesAndNumQuestions(trQs, name, numQs);
  51.     Question *qPtr[numQs];
  52.     for(int i=0;i<numQs;i++)
  53.         qPtr[i] = new Question(trQs); //I can't figure out how to make a function perform this for loop without causing the program to crash
  54.     randomize(qPtr,numQs);
  55.     printTrivia(qPtr,numQs,score,name,NUMBEROFQUESTIONSTOASK);
  56.     showResults(qPtr,name);
  57.     destroy(qPtr,numQs);
  58.     pressSpaceToUnpause();
  59.     return 0;
  60. }
  61.  
  62. //..............................................................................................
  63. //definition of getNamesAndNumQuestions. Does exactly what it says it does. It gets names, and
  64. //the number of questions.
  65. //At 2 am I accidentally deleted my entire main.cpp file. I re-wrote this entire file. In about
  66. //one hour. Yeah, it sucked. My previous submission used another text file to read in names and
  67. //the contestants answers. however i re-read the directions and saw that there should only be
  68. //4 files submitted, so I realized I was doing it wrong. So I guess re-writing the entire main.cpp
  69. //file wasn't ALL bad. I had to modify the class file too of course since its functions had the
  70. //other input file in their parameter lists, etc.
  71. //Anyways I left some lines from the previous program commented out, just as a reminder to how
  72. //much it sucks to accidentally lose work
  73. //..............................................................................................
  74. void getNamesAndNumQuestions(ifstream &trQs, string *name, int &numQs)
  75. {
  76.     trQs.open("TriviaQuestions.txt");
  77.     cout << "Welcome to the trivia game! You will be presented\nwith a question and four answers.\nOne of them is right; you must enter either\n1, 2, 3 or 4 as an answer.\nPlayer 1, enter your name!\n";
  78.     getline(cin,name[0]);
  79.     cout << "Cool " << name[0] << "! Now, player 2, enter your name!\n";
  80.     getline(cin,name[1]);
  81.     //trAs.open("TriviaAnswers.txt");
  82.     //getline(trAs, name1);
  83.     //getline(trAs, name2);
  84.     trQs >> numQs;
  85.     trQs.clear(); //mixing getline with cin produces bad things. .clear and .ignore fix those things.
  86.     trQs.ignore(100,'\n');
  87.     cout << endl << endl;
  88.     return;
  89. }
  90. //..............................................................................................
  91. //simple function just destroys the newly allocated question objects. runs at program's end.
  92. //..............................................................................................
  93. void destroy(Question **qPtr, int numQs)
  94. {
  95.     for(int i=0;i<numQs;i++)
  96.         delete qPtr[i];
  97.     return;
  98. }
  99. //..............................................................................................
  100. //this function prints questions and answers, as well as prints the contestant's choice answer.
  101. //of course their choice is random but who cares this program has to look realistic!
  102. //..............................................................................................
  103. void printTrivia(Question **qPtr, int numQs, int *score, string *name, int NUMBEROFQUESTIONSTOASK)
  104. {
  105.     for(int i = 0; i<NUMBEROFQUESTIONSTOASK; i++)
  106.     {
  107.         (*qPtr[i]).printQandA();
  108.         cout << endl;
  109.         (*qPtr[i]).getAnswer(name);
  110.         cout << endl;
  111.     }
  112.     return;
  113. }
  114. //..............................................................................................
  115. //this function randomly swaps addresses that the pointers in the pointer array qPtr are
  116. //pointing to. This effectively randomizes the questions. Since only the first 10 questions are
  117. //used in the function "printTrivia", this randomizes which questions will be asked. After all
  118. //if there are more than 10 questions, one will randomly be placed in an element over 10. So it
  119. //won't be called by printTrivia.
  120. //..............................................................................................
  121. void randomize(Question **qPtr, int numQs)
  122. {
  123.     Question *temp;
  124.     int randomElement;
  125.     for(int i = 0; i < numQs; i++)
  126.     {
  127.         randomElement = rand()%numQs;
  128.         temp = qPtr[i];
  129.         qPtr[i] = qPtr[randomElement];
  130.         qPtr[randomElement] = temp;
  131.     }
  132.     return;
  133. }
  134. //..............................................................................................
  135. //This function takes the static score from class Question and finds out which one is higher,
  136. //score[0] or score[1]. That's how the program determines the winner of this trivia game.
  137. //..............................................................................................
  138. void showResults(Question **qPtr, string *name)
  139. {
  140.     cout << "////////////////////////////////////////////////////////////" << endl; //helps seperate results from previous questions section
  141.     cout << name[0] << " scored " << qPtr[0]->getscore1() << " point"; //checks to see if point should be points. grammar is nice
  142.     if(qPtr[0]->getscore1()==0 || qPtr[0]->getscore1()>1)
  143.         cout << "s.\n";
  144.     else
  145.         cout << ".\n";
  146.     cout << name[1] << " scored " << qPtr[0]->getscore2() << " point";
  147.      if(qPtr[0]->getscore2()==0 || qPtr[0]->getscore2()>1)
  148.         cout << "s.\n";
  149.     else
  150.         cout << ".\n";
  151.     if(qPtr[0]->getscore1()>qPtr[0]->getscore2())
  152.         cout << name[0] << " won!\n";
  153.     else if(qPtr[0]->getscore1()<qPtr[0]->getscore2())
  154.         cout << name[1] << " won!\n";
  155.     else
  156.         cout << "It's a tie! Everyone wins! Or everyone loses. It depends\non your outlook on life!\n";
  157.     cout << "////////////////////////////////////////////////////////////" << endl;
  158. }
  159. //..............................................................................................
  160. //This function, pressSPaceToUnpause makes it so the user has to hit any button to end the program
  161. //Even hitting return works. This way the program can run when being opened without the use
  162. //of an IDE without automatically closing at the end.
  163. //..............................................................................................
  164. void pressSpaceToUnpause()
  165. {
  166.     cin.get();
  167.     return;
  168. }
  169. //..............................................................................................
Advertisement
Add Comment
Please, Sign In to add comment