// // Created by caleb on 4/22/24. // #include "TriviaGame.h" #include "simpleMenu.h" #include #include std::vector answerTracker; // Trivia Question constructor TriviaGame::TriviaQuestion::TriviaQuestion(std::string triviaQuestion, std::vector triviaOptions, int correct) { this->triviaQuestion = std::move(triviaQuestion); this->triviaOptions = std::move(triviaOptions); this->correctQuestion = correct; } // Trivia Questions static std::vector triviaQuestions = { TriviaGame::TriviaQuestion("What does C++ stand for?", {"C++ Plus", "C Plus Plus", "C Plus", "C++ Plus Plus"}, 1), TriviaGame::TriviaQuestion("Which of the following is not a C++ keyword?", {"class", "malloc", "virtual", "private"}, 1), TriviaGame::TriviaQuestion("What is the result of 5 + 3 * 2?", {"17", "11", "10", "13"}, 1), TriviaGame::TriviaQuestion("Which header file must be included to use cout?", {"", "", "", ""}, 0), TriviaGame::TriviaQuestion("What is the size of 'char' data type in C++?", {"1 byte", "2 bytes", "4 bytes", "Depends on the compiler"}, 0), TriviaGame::TriviaQuestion("What does the 'new' operator do?", {"Allocates memory on the heap", "Allocates memory on the stack", "Deletes memory", "None of the above"}, 0), TriviaGame::TriviaQuestion("What is the correct way to declare a pointer in C++?", {"int* ptr;", "int ptr;", "int ptr();", "int ptr[];"}, 0), TriviaGame::TriviaQuestion("What is the scope resolution operator in C++?", {"::", ".", ":", "->"}, 0), TriviaGame::TriviaQuestion("Which of the following is not a C++ standard library container?", {"List", "Vector", "Set", "Map"}, 0), TriviaGame::TriviaQuestion("What does the 'delete' operator do?", {"Deletes memory allocated by 'new'", "Deletes memory allocated by 'malloc'", "Deallocates memory on the stack", "None of the above"}, 0), TriviaGame::TriviaQuestion( "What is the output of the following code?\n\nint x = 5;\nint& ref = x;\nref = 10;\nstd::cout << x;", {"5", "10", "Error", "Undefined behavior"}, 1), TriviaGame::TriviaQuestion("What does 'polymorphism' mean in C++?", {"Having multiple constructors", "Having multiple data types", "Having multiple functions with the same name but different parameters", "Having multiple return types"}, 2), TriviaGame::TriviaQuestion("What is the main difference between 'struct' and 'class' in C++?", {"Struct can have member functions, whereas class cannot", "Class can have member functions, whereas struct cannot", "There is no difference", "Struct members are public by default, whereas class members are private by default"}, 3), TriviaGame::TriviaQuestion("What does the 'static' keyword mean in C++?", {"Memory allocation on the heap", "Memory allocation on the stack", "Shared across all instances of a class", "Dynamic memory allocation"}, 2), TriviaGame::TriviaQuestion("What is a destructor in C++?", {"A function that creates objects", "A function that is called when an object goes out of scope", "A function that initializes objects", "A function that overloads operators"}, 1), TriviaGame::TriviaQuestion("Which of the following is not a valid way to pass arguments to a function in C++?", {"By value", "By reference", "By pointer", "By name"}, 3), TriviaGame::TriviaQuestion("What does the 'const' keyword mean in C++?", {"The value cannot be modified", "The variable cannot be accessed", "The variable is static", "The variable is global"}, 0), TriviaGame::TriviaQuestion("What is the purpose of 'iostream' in C++?", {"For input/output operations", "For mathematical operations", "For string operations", "For memory operations"}, 0), TriviaGame::TriviaQuestion("What is an 'iterator' in C++?", {"A pointer-like object that can traverse elements in a container", "A data type used for integer operations", "A data structure used for storing multiple values", "A function that returns a value"}, 0), TriviaGame::TriviaQuestion("What is the 'sizeof' operator used for in C++?", {"To determine the size of a variable or type", "To perform arithmetic operations", "To allocate memory", "To convert data types"}, 0) }; int countCorrectAnswers() { int count = 0; for (int i = 0; i < answerTracker.size(); ++i) { if (answerTracker[i]) { count++; } } return count; } void resetCorrectAnswers() { for (auto &&val: answerTracker) { val = false; } } int ensureUnique(int randInt, int range) { do { if (countCorrectAnswers() >= range) { resetCorrectAnswers(); } if (answerTracker[randInt]) { randInt = rand() % range; } } while(answerTracker[randInt]); return randInt; } void TriviaGame::triviaGameLoop() { int triviaQuestionSelector; int range = triviaQuestions.size(); for (int i = 0; i < range; ++i) { answerTracker.emplace_back(false); } int answersCorrect = 0, answersIncorrect = 0; std::string menuTitle; do { triviaQuestionSelector = ensureUnique(rand() % range, range); TriviaGame::TriviaQuestion currentQuestion = triviaQuestions[triviaQuestionSelector]; menuTitle = "Current Question #: " + std::to_string(triviaQuestionSelector) + "\nQuestions correct: " + std::to_string(answersCorrect) + "\nAnswers incorrect: " + std::to_string(answersIncorrect) + "\n" + currentQuestion.triviaQuestion; int userAnswer = simpleMenu(currentQuestion.triviaOptions, menuTitle); if (userAnswer == currentQuestion.correctQuestion) { answersCorrect++; answerTracker[triviaQuestionSelector] = true; } else { answersIncorrect++; } } while (true); }