Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stdio.h"
- #include "Answer.h"
- #include "Question.h"
- #include <vector>
- #include "time.h"
- void waitForEnter()
- {
- while(1)
- {
- if( '\n' == getchar() )
- {
- break;
- }
- }
- return;
- }
- int main()
- {
- int questionsAnswered = 0;
- int score = 0;
- int answeredNumber = -1;
- Answer qOneAnswerOne("5", true);
- Answer qOneAnswerTwo("6", false);
- Answer qOneAnswerThree("-1", false);
- std::vector<Answer> qOneAnswers;
- qOneAnswers.push_back(qOneAnswerOne);
- qOneAnswers.push_back(qOneAnswerTwo);
- qOneAnswers.push_back(qOneAnswerThree);
- Question questionOne("What is 2+3?", qOneAnswers);
- Answer qTwoAnswerOne("5", false);
- Answer qTwoAnswerTwo("6", true);
- Answer qTwoAnswerThree("-1", false);
- std::vector<Answer> qTwoAnswers;
- qTwoAnswers.push_back(qTwoAnswerOne);
- qTwoAnswers.push_back(qTwoAnswerTwo);
- qTwoAnswers.push_back(qTwoAnswerThree);
- Question questionTwo("What is 2x3?", qTwoAnswers);
- Answer qThreeAnswerOne("42", true);
- Answer qThreeAnswerTwo("55", false);
- Answer qThreeAnswerThree("huh?", false);
- std::vector<Answer> qThreeAnswers;
- qThreeAnswers.push_back(qThreeAnswerOne);
- qThreeAnswers.push_back(qThreeAnswerTwo);
- qThreeAnswers.push_back(qThreeAnswerThree);
- Question questionThree("What is the answer to Life the Universe and Everything?", qThreeAnswers);
- std::vector<Question> lstQuestions;
- lstQuestions.push_back(questionOne);
- lstQuestions.push_back(questionTwo);
- lstQuestions.push_back(questionThree);
- srand(time(NULL));
- bool exitRequested = false;
- while(!exitRequested)
- {
- Question* ptrCurrentQuestion = &lstQuestions[rand()% (int) lstQuestions.size()];
- ptrCurrentQuestion->displayQuestion();
- ptrCurrentQuestion->displayAnswerOptions();
- std::cout << "Enter your answer or -1 to exit: ";
- while( !(std::cin >> answeredNumber) || (answeredNumber < -1) ||
- (answeredNumber > (int) ptrCurrentQuestion->getAnswers().size()-1) )
- {
- std::cin.clear(); //clear the error
- std::cin.ignore(10000, '\n'); //ignore the entered garbage
- std::cout << "Please enter a valid answer: ";
- }
- if(answeredNumber == -1)
- {
- exitRequested = true;
- }
- else if(ptrCurrentQuestion->isRightAnswer(answeredNumber))
- {
- std::cout << "Correct! :)\n";
- score++;
- questionsAnswered++;
- }
- else
- {
- std::cout << "Incorrect :/\n";
- questionsAnswered++;
- }
- std::cout << "Score: " << score << " ";
- std::cout << "Questions Answered: " << questionsAnswered << std::endl;
- }
- std::cin.ignore(10000, '\n');
- std::cout << "Press enter to exit...";
- waitForEnter();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement