Advertisement
zCool

main.cpp

May 23rd, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include "stdio.h"
  3. #include "Answer.h"
  4. #include "Question.h"
  5. #include <vector>
  6. #include "time.h"
  7.  
  8. void waitForEnter()
  9. {
  10.     while(1)
  11.     {
  12.         if( '\n' == getchar() )
  13.         {
  14.             break;
  15.         }
  16.     }
  17.     return;
  18. }
  19.  
  20. int main()
  21. {
  22.     int questionsAnswered = 0;
  23.     int score = 0;
  24.     int answeredNumber = -1;
  25.     Answer qOneAnswerOne("5", true);
  26.     Answer qOneAnswerTwo("6", false);
  27.     Answer qOneAnswerThree("-1", false);
  28.     std::vector<Answer> qOneAnswers;
  29.     qOneAnswers.push_back(qOneAnswerOne);
  30.     qOneAnswers.push_back(qOneAnswerTwo);
  31.     qOneAnswers.push_back(qOneAnswerThree);
  32.     Question questionOne("What is 2+3?", qOneAnswers);
  33.  
  34.     Answer qTwoAnswerOne("5", false);
  35.     Answer qTwoAnswerTwo("6", true);
  36.     Answer qTwoAnswerThree("-1", false);
  37.     std::vector<Answer> qTwoAnswers;
  38.     qTwoAnswers.push_back(qTwoAnswerOne);
  39.     qTwoAnswers.push_back(qTwoAnswerTwo);
  40.     qTwoAnswers.push_back(qTwoAnswerThree);
  41.     Question questionTwo("What is 2x3?", qTwoAnswers);
  42.  
  43.     Answer qThreeAnswerOne("42", true);
  44.     Answer qThreeAnswerTwo("55", false);
  45.     Answer qThreeAnswerThree("huh?", false);
  46.     std::vector<Answer> qThreeAnswers;
  47.     qThreeAnswers.push_back(qThreeAnswerOne);
  48.     qThreeAnswers.push_back(qThreeAnswerTwo);
  49.     qThreeAnswers.push_back(qThreeAnswerThree);
  50.     Question questionThree("What is the answer to Life the Universe and Everything?", qThreeAnswers);
  51.  
  52.     std::vector<Question> lstQuestions;
  53.     lstQuestions.push_back(questionOne);
  54.     lstQuestions.push_back(questionTwo);
  55.     lstQuestions.push_back(questionThree);
  56.  
  57.     srand(time(NULL));
  58.     bool exitRequested = false;
  59.  
  60.     while(!exitRequested)
  61.     {
  62.    
  63.         Question* ptrCurrentQuestion = &lstQuestions[rand()% (int) lstQuestions.size()];
  64.  
  65.         ptrCurrentQuestion->displayQuestion();
  66.         ptrCurrentQuestion->displayAnswerOptions();
  67.    
  68.         std::cout << "Enter your answer or -1 to exit: ";
  69.         while( !(std::cin >> answeredNumber) || (answeredNumber < -1) ||
  70.             (answeredNumber > (int) ptrCurrentQuestion->getAnswers().size()-1) )
  71.         {
  72.             std::cin.clear(); //clear the error
  73.             std::cin.ignore(10000, '\n'); //ignore the entered garbage
  74.             std::cout << "Please enter a valid answer: ";
  75.         }
  76.  
  77.         if(answeredNumber == -1)
  78.         {
  79.             exitRequested = true;
  80.         }
  81.         else if(ptrCurrentQuestion->isRightAnswer(answeredNumber))
  82.         {
  83.             std::cout << "Correct! :)\n";
  84.             score++;
  85.             questionsAnswered++;
  86.         }
  87.         else
  88.         {
  89.             std::cout << "Incorrect :/\n";
  90.             questionsAnswered++;
  91.         }
  92.  
  93.         std::cout << "Score: " << score << "  ";
  94.         std::cout << "Questions Answered: " << questionsAnswered << std::endl;
  95.     }
  96.     std::cin.ignore(10000, '\n');
  97.     std::cout << "Press enter to exit...";
  98.     waitForEnter();
  99.    
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement