Last active
November 25, 2018 11:10
-
-
Save blongho/7f62bc75e7dac7c9fe081123a4ea13ff to your computer and use it in GitHub Desktop.
c++ multiple choice question created by blongho - https://repl.it/@blongho/c-multiple-choice-question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file : Answer.hpp | |
* @author: blongho | |
* @since : 2018-11-25 | |
* @brief : A class to hold an Answer for use in a multiple choice Question | |
**/ | |
#ifndef ANSWER_H | |
#define ANSWER_H | |
#include <iostream> | |
/** | |
* This class was originally intended to be a template but it resulted to over complication | |
* Just make sure all your answers values are std::string. It makes a whole lot | |
* easier implementation and use | |
* | |
* Usage: | |
* Answer answer1("value"); // results to value=value, isCorrect() = false; | |
* Answer answer2("2", true); // results to value=2, isCorrect() = true; | |
* Answer answer3("true", true) // results to value=true, isCorrect() = true; | |
* Answer answer4("5.6", false); // results to value=5.6, isCorrect() = false; | |
*/ | |
class Answer{ | |
private: | |
std::string value; | |
bool correct; | |
public: | |
Answer()=default; | |
Answer(const std::string &val, bool status = false){ | |
value = val; | |
correct = status; | |
} | |
bool isCorrect() const{ | |
return correct; | |
} | |
std::string getValue() const{ | |
return value; | |
} | |
void setValue(const std::string &val){ | |
value = val; | |
} | |
void setCorrect(const bool &status){ | |
correct = status; | |
} | |
void printOn(std::ostream &os=std::cout){ | |
os << "ANSWER: value=" << value << ", is correct? " << std::boolalpha << correct << "\n"; | |
} | |
// to be used in Question class where we display just the option | |
void display(std::ostream &os=std::cout){ | |
os << value << "\n"; | |
} | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file : Main.cpp | |
* @author: blongho | |
* @since : 2018-11-25 | |
* @brief : Demonstration of use of Question and Answer classes | |
**/ | |
#include <iostream> | |
#include "Question.hpp" | |
#include <string> | |
int main() { | |
Answer op1("2"), op2("5",true), op3("6"); | |
std::vector<Answer> answers{op1, op2, op3}; | |
Question q1("How many questions are there?", answers); | |
//q1.run(); | |
Question q2("What is the season in Sweden between December and March?", | |
{Answer("autumn"), Answer("spring"), Answer("winter", true), Answer("summer")}); | |
// q2.run(); | |
Question q3("Barack Obama was the US President in 2017",{Answer("true"), Answer("false", true)}); | |
// q3.run(); | |
// run all questions at once and then mark them | |
std::vector<Question> questions{q1, q2, q3}; | |
int i = 1; | |
for(auto &q: questions){ | |
std::cout << i << ". "; | |
q.run(); | |
i++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file : Question.hpp | |
* @author: blongho | |
* @since : 2018-11-25 | |
* @brief : A class to hold an Question and its answers for use in a multiple choice exam | |
**/ | |
#ifndef QUESTION_H | |
#define QUESTION_H | |
#include "Answer.hpp" | |
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <algorithm> // std::random_shuffle | |
#include <exception> | |
class Question { | |
private: | |
std::string question; | |
std::vector<Answer> options; | |
void mark(const std::string &response) | |
{ | |
std::cout << std::endl; | |
if(response == correctAnswer()){ | |
std::cout << "Your choice, '" << response << "' is correct" << std::endl; | |
} | |
else | |
{ | |
std::cout << "Your choice, '" << response << "' is wrong" << std::endl; | |
int i = 1; | |
for(auto &c: options){ | |
std::cout << i << ": "; | |
c.printOn(); | |
i++; | |
} | |
} | |
std::cout << std::endl; | |
} | |
std::string correctAnswer() | |
{ | |
std::string answer; | |
for(auto &ans: options){ | |
if(ans.isCorrect()){ | |
answer = ans.getValue(); | |
break; | |
} | |
} | |
return answer; | |
} | |
void display(std::ostream &os=std::cout) | |
{ | |
os << question << "\n"; | |
int i = 1; | |
std::random_shuffle ( options.begin(), options.end()); | |
for(auto &ans: options){ | |
os << i << ": " ; | |
ans.display(); | |
i++; | |
} | |
os << std::endl; | |
} | |
public: | |
Question(){}; | |
Question(const std::string &q, const std::vector<Answer> &answers) | |
:question(q), options(answers){} | |
void setQuestion(const std::string &quest){ | |
question = quest; | |
} | |
void setOptions(const std::vector<Answer> &answers){ | |
options = answers; | |
} | |
std::vector<Answer> getOptions() const{ | |
return options; | |
} | |
std::string getQuestion() const{ | |
return question; | |
} | |
void run() | |
{ | |
int intResponse{0}; | |
display(); // show the question and options | |
// get valid response | |
do{ | |
std::string response; | |
std::cout << "Your choice: "; | |
std::getline(std::cin, response); | |
try{ | |
intResponse = stoi(response); | |
}catch(std::exception &e){ | |
std::cout << "Bad value entered for response [" << response << "]" << std::endl; | |
} | |
if( intResponse > options.size() || intResponse < 0){ | |
std::cout << "Your answer should be integer between 1 and " | |
<< options.size() << std::endl; | |
} | |
}while(intResponse < 1 || intResponse > options.size()); | |
// still do control before accessing values in the vector | |
if( intResponse <= options.size() && intResponse >0){ | |
std::string choice = options[ intResponse-1].getValue(); | |
mark(choice); // constrol and print response | |
}else{ | |
std::cout << "Your answer should be integer between 1 and " << options.size() | |
<< std::endl; | |
} | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an attempt to solve Linking questions to numbers from cplusplus.com forum