Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///..............................................................
- ///
- ///This implementation file is meant to be used with the Question
- ///class.
- ///
- ///Joseph Burger
- ///
- #include "Question.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- int Question::score1 = 0;
- int Question::score2 = 0;
- int Question::whatNumQuestion = 1;
- Question::Question(ifstream &triviaQuestions) //this constructor initializes strings from the input file
- {
- getline(triviaQuestions, question);
- for(int i = 0; i < 4; i++)
- getline(triviaQuestions,answer[i]);
- triviaQuestions >> correct;
- triviaQuestions.clear();
- triviaQuestions.ignore(100,'\n');
- }
- void Question::printQandA() //this function prints question and answers to screen so contestant can read them and make a choice
- {
- cout << "######################" << endl; //this helps contestants to distinguish each turn from other turns. having a giant header thing helps with that.
- cout << "# QUESTION NUMBER " << setw(2) << whatNumQuestion << " #" << endl;
- cout << "######################" << endl;
- cout << question << endl;
- for(int i=0;i<4;i++)
- cout << answer[i] << endl;
- }
- //Definition of Question member function getAnswer. It takes an answer from input, matches it to
- //the correct answer stored inside the question object, and then increments the player's score
- //or doesn't. It takes name1 and name2 so it can write a nice message to the console.
- void Question::getAnswer(string *name) //return true if answer right, return false if answer wrong.
- {
- int contestantAnswer;
- cout << "It's "; //a message prompting the contestant to put in an answer!
- if(whatNumQuestion%2!=0)
- cout << name[0] << "'s turn!\n";
- else
- cout << name[1] << "'s turn!\n";
- cin >> contestantAnswer;
- while(contestantAnswer>4 || contestantAnswer<1) //input validation!
- {
- cout << "Silly goose, that's not valid! Now enter either 1, 2, 3 or 4.\n"; //got to warn the user! GOT TO.
- cin.clear();
- cin.ignore(100,'\n');
- cin >> contestantAnswer; //I converted the getline to cin and am using an int now. The program is smaller using an int rather than a string with a switch statement
- }
- cin.clear(); //just making sure getline doesn't get messed up!
- cin.ignore(100,'\n');
- if(contestantAnswer==correct && whatNumQuestion%2!=0) //if odd; player 1
- score1++;
- else if(contestantAnswer==correct && whatNumQuestion%2==0) //if even; player 2
- score2++;
- //cout << "The correct answer was " << correct << "; ";
- // if(whatNumQuestion%2!=0) //I just realized there's no need to say what someone answered, because the cin input stays on screen anyways.
- // cout << name[0] << " answered " << contestantAnswer << ".\n";
- // if(whatNumQuestion%2==0)
- // cout << name[1] << " answered " << contestantAnswer << ".\n";
- whatNumQuestion++; //increment question counter to keep track of which question we're on
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment