I want to build a quiz and want to put in a database/libary/vector or array. Can anyone point me to just the tutorial as to how to do this? I have been following some youtube videos and are stuck on adding libraries. The instructions were over 2 years old and do not work. Not sure if that is the way to go or not.
I can show you a quick example of using a vector. Using a database for this like SQL would be overkill. I assume you realistically just want a file to save a quiz in to load it back up later? It's not clear what the requirements are.
// Example program
#include <iostream>
#include <string>
#include <vector>
struct QuizQuestion {
std::string prompt;
std::string answer;
};
int main()
{
std::vector<QuizQuestion> questions = {
{ "What is the largest country by area?", "Russia" },
{ "What is the largest country by population?", "China" },
{ "How much wood would a woodchuck chuck if a woodchuck could chuck wood?", "So much wood" }
};
// add another question dynamically (say, from a file)
questions.push_back( { "What number am I thinking of?", "42" } );
std::cout << "Quiz!\n";
for (constauto& question : questions)
{
std::cout << "Question: " << question.prompt << ' ';
std::string answer;
std::getline(std::cin, answer);
if (answer == question.answer)
{
std::cout << "Correct!\n";
}
else
{
std::cout << "Incorrect. The answer was: " << question.answer << '\n';
}
}
}
Quiz!
Question: What is the largest country by area? Russia
Correct!
Question: What is the largest country by population? China
Correct!
Question: How much wood would a woodchuck chuck if a woodchuck could chuck wood? 42
Incorrect. The answer was: So much wood
Question: What number am I thinking of? 42
Correct!
The more specific your question, and the more effort you show, usually the better help you'll get.