Created
March 2, 2021 14:45
-
-
Save cmarchena/7c75d6755b3211661f9e272dce14ede9 to your computer and use it in GitHub Desktop.
A C++ Quiz App that demonstrate the use of map as data structure
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
#include <iostream> | |
#include <string> | |
#include <map> | |
int score =0, answer; | |
struct Question { | |
std::string text; | |
std::string options[4]; | |
int correct_answer; | |
}; | |
std::map <int, Question> data = | |
{ | |
{0,{"Who is the author of C++?", {"Linus Torvalds", "Ada Lovelace", "Bjarne Stroustrup", "Brendan Eich"}, 3}}, | |
{1, {"The year when C++ first appeared: ", {"2021", "1991", "2005", "1985"}, 4}}, | |
{2, {"What is C++ most current version? (in 2021)", {"C++21", "C++20", "C++1", "C++17"},2}}, | |
{3, {"Which one of the following is a valid extension for C++ file?",{".cpp", ".xml", ".ts", ".rs"},1}}, | |
{4, {"C++ Author was born in:", {"Amsterdam, Netherland", "Stockholm, Sweden", "Madrid, Spain", "Aarhus, Denmark"}, 4}} | |
}; | |
class Quiz{ | |
public: | |
void printQuestion(std::string options[4]){ | |
for(int i = 0; i<4; i++){ | |
std::cout << (i+1)<< ".- " << options[i] << std::endl; | |
} | |
} | |
void checkIsCorrect(int answer, int correct_answer){ | |
if (correct_answer == answer) | |
{ score++; | |
std::cout << "Your selection is correct\n"; | |
} | |
else | |
{ | |
std::cout<< "Your selection is wrong. Correct answer is " << correct_answer << std::endl; | |
} | |
} | |
void start(){ | |
std::cout << "Welcome to the Quiz App!!\n"; | |
auto it = data.begin(); | |
while(it != data.end()){ | |
std::cout<< it->second.text<<std::endl; | |
printQuestion(it->second.options); | |
std::cin >> answer; | |
checkIsCorrect(answer, it->second.correct_answer); | |
it++; | |
} | |
if(score ==5){ | |
std::cout << "End of game. You got all " << score << " of them! Perfect Score!"<< std::endl; | |
}else{ | |
std::cout << "End of game. Your final score is " << score << std::endl; | |
} | |
} | |
}; | |
int main() | |
{ | |
Quiz quiz; | |
quiz.start(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment