Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TriviaGame
29 changes: 29 additions & 0 deletions QuestionClasses/MultipleChoiceQuestion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "MultipleChoiceQuestion.h"
#include <iostream>

MultipleChoiceQuestion::MultipleChoiceQuestion(const string& prompt, const vector<string>& choices, int correctChoice)
: Question(prompt), correctChoice(correctChoice) {
if (choices.size() > 4 || choices.size() < 2) {
throw invalid_argument("MultipleChoiceQuestion must have between 2 and 4 choices.");
}
this->choices = choices;
if (correctChoice < 1 || correctChoice > choices.size()) {
throw out_of_range("Correct choice must be a valid option index (1-based).");
}
}

void MultipleChoiceQuestion::displayQuestion() const {
cout << prompt << endl;
for (size_t i = 0; i < choices.size(); ++i) {
cout << i + 1 << ". " << choices[i] << endl;
}
}

bool MultipleChoiceQuestion::checkAnswer(const string& answer) const {
try {
int choice = stoi(answer);
return choice == correctChoice;
} catch (...) {
return false;
}
}
21 changes: 21 additions & 0 deletions QuestionClasses/MultipleChoiceQuestion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MULTIPLE_CHOICE_QUESTION_H
#define MULTIPLE_CHOICE_QUESTION_H

#include "Question.h"
#include <vector>
#include <stdexcept>

class MultipleChoiceQuestion : public Question {
private:
vector<string> choices;
int correctChoice;

public:
MultipleChoiceQuestion(const string& prompt, const vector<string>& choices, int correctChoice);

void displayQuestion() const override;

bool checkAnswer(const string& answer) const override;
};

#endif
21 changes: 21 additions & 0 deletions QuestionClasses/OneWordResponseQuestion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "OneWordResponseQuestion.h"
#include <iostream>
#include <algorithm>

OneWordResponseQuestion::OneWordResponseQuestion(const string& prompt, const string& correctAnswer)
: Question(prompt), correctAnswer(correctAnswer) {}

void OneWordResponseQuestion::displayQuestion() const {
cout << prompt << " (Answer in one word)" << endl;
}

bool OneWordResponseQuestion::checkAnswer(const string& answer) const {
string lowerAnswer = answer;
string lowerCorrectAnswer = correctAnswer;

// Convert both to lowercase
for (auto& ch : lowerAnswer) ch = tolower(ch);
for (auto& ch : lowerCorrectAnswer) ch = tolower(ch);

return lowerAnswer == lowerCorrectAnswer;
}
18 changes: 18 additions & 0 deletions QuestionClasses/OneWordResponseQuestion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ONE_WORD_RESPONSE_QUESTION_H
#define ONE_WORD_RESPONSE_QUESTION_H

#include "Question.h"

class OneWordResponseQuestion : public Question {
private:
string correctAnswer;

public:
OneWordResponseQuestion(const string& prompt, const string& correctAnswer);

void displayQuestion() const override;

bool checkAnswer(const string& answer) const override;
};

#endif
21 changes: 21 additions & 0 deletions QuestionClasses/Question.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef QUESTION_H
#define QUESTION_H

#include <string>
using namespace std;

class Question {
protected:
string prompt;

public:
Question(const string& prompt) : prompt(prompt) {}

virtual ~Question() {}

virtual void displayQuestion() const = 0;

virtual bool checkAnswer(const string& answer) const = 0;
};

#endif
21 changes: 21 additions & 0 deletions QuestionClasses/TrueFalseQuestion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "TrueFalseQuestion.h"
#include <iostream>

TrueFalseQuestion::TrueFalseQuestion(const string& prompt, bool correctAnswer)
: Question(prompt), correctAnswer(correctAnswer) {}

void TrueFalseQuestion::displayQuestion() const {
cout << prompt << " (True/False)" << endl;
}

bool TrueFalseQuestion::checkAnswer(const string& answer) const {
string lowerAnswer = answer;
for (auto& ch : lowerAnswer) ch = tolower(ch);

if (lowerAnswer == "true" || lowerAnswer == "t") {
return correctAnswer;
} else if (lowerAnswer == "false" || lowerAnswer == "f") {
return !correctAnswer;
}
return false;
}
18 changes: 18 additions & 0 deletions QuestionClasses/TrueFalseQuestion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef TRUE_FALSE_QUESTION_H
#define TRUE_FALSE_QUESTION_H

#include "Question.h"

class TrueFalseQuestion : public Question {
private:
bool correctAnswer;

public:
TrueFalseQuestion(const string& prompt, bool correctAnswer);

void displayQuestion() const override;

bool checkAnswer(const string& answer) const override;
};

#endif
31 changes: 31 additions & 0 deletions QuestionClasses/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "MultipleChoiceQuestion.h"
#include "TrueFalseQuestion.h"
#include "OneWordResponseQuestion.h"
#include <iostream>
#include <memory>

using namespace std;

int main() {
// Multiple Choice
try {
vector<string> choices = {"Red", "Blue", "Green", "Yellow"};
MultipleChoiceQuestion mcq("What is the color of the sky?", choices, 2);
mcq.displayQuestion();
cout << (mcq.checkAnswer("2") ? "Correct!" : "Incorrect.") << endl;
} catch (const exception& e) {
cerr << e.what() << endl;
}

// True/False
TrueFalseQuestion tfq("The Earth is flat.", false);
tfq.displayQuestion();
cout << (tfq.checkAnswer("false") ? "Correct!" : "Incorrect.") << endl;

// One Word Response
OneWordResponseQuestion owq("What is the capital of France?", "Paris");
owq.displayQuestion();
cout << (owq.checkAnswer("paris") ? "Correct!" : "Incorrect.") << endl;

return 0;
}