putting quiz questions and answer in database?

Write your question here.

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.

Thanks in advance.
What have you tried so far?

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.

Anyway, here's a small example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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 (const auto& 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.
Last edited on
Topic archived. No new replies allowed.