candyapplecorn

Question.cpp

Nov 20th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. ///..............................................................
  2. ///
  3. ///This implementation file is meant to be used with the Question
  4. ///class.
  5. ///
  6. ///Joseph Burger
  7. ///
  8. #include "Question.h"
  9. #include <fstream>
  10. #include <iostream>
  11. #include <string>
  12. #include <cstdlib>
  13. #include <iomanip>
  14. using namespace std;
  15.  
  16. int Question::score1 = 0;
  17. int Question::score2 = 0;
  18. int Question::whatNumQuestion = 1;
  19.  
  20. Question::Question(ifstream &triviaQuestions) //this constructor initializes strings from the input file
  21. {
  22.     getline(triviaQuestions, question);
  23.     for(int i = 0; i < 4; i++)
  24.         getline(triviaQuestions,answer[i]);
  25.     triviaQuestions >> correct;
  26.     triviaQuestions.clear();
  27.     triviaQuestions.ignore(100,'\n');
  28. }
  29.  
  30. void Question::printQandA() //this function prints question and answers to screen so contestant can read them and make a choice
  31. {
  32.     cout << "######################" << endl; //this helps contestants to distinguish each turn from other turns. having a giant header thing helps with that.
  33.     cout << "# QUESTION NUMBER " << setw(2) << whatNumQuestion << " #" << endl;
  34.     cout << "######################" << endl;
  35.     cout << question << endl;
  36.     for(int i=0;i<4;i++)
  37.         cout << answer[i] << endl;
  38. }
  39. //Definition of Question member function getAnswer. It takes an answer from input, matches it to
  40. //the correct answer stored inside the question object, and then increments the player's score
  41. //or doesn't. It takes name1 and name2 so it can write a nice message to the console.
  42. void Question::getAnswer(string *name) //return true if answer right, return false if answer wrong.
  43. {
  44.     int contestantAnswer;
  45.     cout << "It's "; //a message prompting the contestant to put in an answer!
  46.     if(whatNumQuestion%2!=0)
  47.         cout << name[0] << "'s turn!\n";
  48.     else
  49.         cout << name[1] << "'s turn!\n";
  50.     cin >> contestantAnswer;
  51.     while(contestantAnswer>4 || contestantAnswer<1) //input validation!
  52.     {
  53.         cout << "Silly goose, that's not valid! Now enter either 1, 2, 3 or 4.\n"; //got to warn the user! GOT TO.
  54.         cin.clear();
  55.         cin.ignore(100,'\n');
  56.         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
  57.     }
  58.     cin.clear(); //just making sure getline doesn't get messed up!
  59.     cin.ignore(100,'\n');
  60.  
  61.     if(contestantAnswer==correct && whatNumQuestion%2!=0) //if odd; player 1
  62.             score1++;
  63.     else if(contestantAnswer==correct && whatNumQuestion%2==0) //if even; player 2
  64.             score2++;
  65.  
  66.     //cout << "The correct answer was " << correct << "; ";
  67. //    if(whatNumQuestion%2!=0) //I just realized there's no need to say what someone answered, because the cin input stays on screen anyways.
  68. //        cout << name[0] << " answered " << contestantAnswer << ".\n";
  69. //    if(whatNumQuestion%2==0)
  70. //        cout << name[1] << " answered " << contestantAnswer << ".\n";
  71.  
  72.     whatNumQuestion++; //increment question counter to keep track of which question we're on
  73.  
  74.     return;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment