Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///..............................................................................................
- ///
- ///TRIVIA GAME
- ///aka: "This Must Be The 6th Time I Have Re-Written This File"
- ///Homework #6
- ///
- ///This is a trivia game program. It uses a class called Question.h! This program used to simlate
- ///a trivia game. In this program, the contestants' answers were random. However in a real trivia
- ///game, contestants don't answer randomly! But I thought the requirements for this assignment didn't say
- ///to make a real trivia game. So I didn't. But then I re-read the requirements a berjillion times and
- ///realized the book was asking for a real trivia game that would take answers from input, aka "cin".
- ///So I remade it AGAIN. Now it's a REAL trivia game, not a fake simulated one. Boy howdy!
- ///
- ///The file TriviaQuestions.txt can be as many questions as you want! It just has to have the number
- ///of questions at the top. It would be possible to make an input file without the number of
- ///questions at the top, but that would be more difficult, and previous input files have
- ///had a number at the top indicating how long a list is, or something similar. So I figured it'd
- ///be okay.
- ///
- ///Joseph Burger
- ///
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <fstream>
- #include <iomanip>
- #include <ctime>
- #include "Question.h"
- using namespace std;
- void getNamesAndNumQuestions(ifstream &trQs, string *name, int &numQs);
- void destroy(Question **qPtr, int numQs);
- void printTrivia(Question **qPtr, int numQs, int *score, string *name, int NUMBEROFQUESTIONSTOASK);
- void randomize(Question **qPtr, int numQs);
- void showResults(Question **qPtr, string *name);
- void pressSpaceToUnpause();
- //..............................................................................................
- //main is responsible for seeding the random number generator, creating dynamically allocated
- //question objects, and calling functions which store questions from question.txt into Question
- //objects, and calls member functions from the Question class and functions from this source
- //file. It throws everything together. It's main().
- //..............................................................................................
- int main()
- {
- 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
- srand(time(NULL));
- ifstream trQs;
- string name[2];
- int numQs, score[2];
- getNamesAndNumQuestions(trQs, name, numQs);
- Question *qPtr[numQs];
- for(int i=0;i<numQs;i++)
- 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
- randomize(qPtr,numQs);
- printTrivia(qPtr,numQs,score,name,NUMBEROFQUESTIONSTOASK);
- showResults(qPtr,name);
- destroy(qPtr,numQs);
- pressSpaceToUnpause();
- return 0;
- }
- //..............................................................................................
- //definition of getNamesAndNumQuestions. Does exactly what it says it does. It gets names, and
- //the number of questions.
- //At 2 am I accidentally deleted my entire main.cpp file. I re-wrote this entire file. In about
- //one hour. Yeah, it sucked. My previous submission used another text file to read in names and
- //the contestants answers. however i re-read the directions and saw that there should only be
- //4 files submitted, so I realized I was doing it wrong. So I guess re-writing the entire main.cpp
- //file wasn't ALL bad. I had to modify the class file too of course since its functions had the
- //other input file in their parameter lists, etc.
- //Anyways I left some lines from the previous program commented out, just as a reminder to how
- //much it sucks to accidentally lose work
- //..............................................................................................
- void getNamesAndNumQuestions(ifstream &trQs, string *name, int &numQs)
- {
- trQs.open("TriviaQuestions.txt");
- 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";
- getline(cin,name[0]);
- cout << "Cool " << name[0] << "! Now, player 2, enter your name!\n";
- getline(cin,name[1]);
- //trAs.open("TriviaAnswers.txt");
- //getline(trAs, name1);
- //getline(trAs, name2);
- trQs >> numQs;
- trQs.clear(); //mixing getline with cin produces bad things. .clear and .ignore fix those things.
- trQs.ignore(100,'\n');
- cout << endl << endl;
- return;
- }
- //..............................................................................................
- //simple function just destroys the newly allocated question objects. runs at program's end.
- //..............................................................................................
- void destroy(Question **qPtr, int numQs)
- {
- for(int i=0;i<numQs;i++)
- delete qPtr[i];
- return;
- }
- //..............................................................................................
- //this function prints questions and answers, as well as prints the contestant's choice answer.
- //of course their choice is random but who cares this program has to look realistic!
- //..............................................................................................
- void printTrivia(Question **qPtr, int numQs, int *score, string *name, int NUMBEROFQUESTIONSTOASK)
- {
- for(int i = 0; i<NUMBEROFQUESTIONSTOASK; i++)
- {
- (*qPtr[i]).printQandA();
- cout << endl;
- (*qPtr[i]).getAnswer(name);
- cout << endl;
- }
- return;
- }
- //..............................................................................................
- //this function randomly swaps addresses that the pointers in the pointer array qPtr are
- //pointing to. This effectively randomizes the questions. Since only the first 10 questions are
- //used in the function "printTrivia", this randomizes which questions will be asked. After all
- //if there are more than 10 questions, one will randomly be placed in an element over 10. So it
- //won't be called by printTrivia.
- //..............................................................................................
- void randomize(Question **qPtr, int numQs)
- {
- Question *temp;
- int randomElement;
- for(int i = 0; i < numQs; i++)
- {
- randomElement = rand()%numQs;
- temp = qPtr[i];
- qPtr[i] = qPtr[randomElement];
- qPtr[randomElement] = temp;
- }
- return;
- }
- //..............................................................................................
- //This function takes the static score from class Question and finds out which one is higher,
- //score[0] or score[1]. That's how the program determines the winner of this trivia game.
- //..............................................................................................
- void showResults(Question **qPtr, string *name)
- {
- cout << "////////////////////////////////////////////////////////////" << endl; //helps seperate results from previous questions section
- cout << name[0] << " scored " << qPtr[0]->getscore1() << " point"; //checks to see if point should be points. grammar is nice
- if(qPtr[0]->getscore1()==0 || qPtr[0]->getscore1()>1)
- cout << "s.\n";
- else
- cout << ".\n";
- cout << name[1] << " scored " << qPtr[0]->getscore2() << " point";
- if(qPtr[0]->getscore2()==0 || qPtr[0]->getscore2()>1)
- cout << "s.\n";
- else
- cout << ".\n";
- if(qPtr[0]->getscore1()>qPtr[0]->getscore2())
- cout << name[0] << " won!\n";
- else if(qPtr[0]->getscore1()<qPtr[0]->getscore2())
- cout << name[1] << " won!\n";
- else
- cout << "It's a tie! Everyone wins! Or everyone loses. It depends\non your outlook on life!\n";
- cout << "////////////////////////////////////////////////////////////" << endl;
- }
- //..............................................................................................
- //This function, pressSPaceToUnpause makes it so the user has to hit any button to end the program
- //Even hitting return works. This way the program can run when being opened without the use
- //of an IDE without automatically closing at the end.
- //..............................................................................................
- void pressSpaceToUnpause()
- {
- cin.get();
- return;
- }
- //..............................................................................................
Advertisement
Add Comment
Please, Sign In to add comment