Quiz in c++

So i have a project with a quiz, and i had it to present it to my teacher, i did it, it worked but the teacher wasn't happy because he said that the code was to easy and to basic, so he told me to remake it and put the question in another file or the answers in another file and make them appear in a random order. ( I'm a beginner so i don't know exactly how to do it,a little bit of help would be helpfull, also i'm not a native english so sorry for the grammar mistakes )


Here are 2 of the question from my quiz translated, i just need a little help understanding how to write them in another file and reading them in a random order ( either the questions either the answers )

string q1 = "";

cout << "q 1:\nHow many neighbours does Romania have? \n\na = '3'\n" << "b = '4'\nc = '5'\nd ="<< " '6'\n" << endl;
cin >> q1;
cout << "\n" << endl;

if (q1 == "C" || q1 == "c")
{
cout << "You answered corectly! +1p \n\n";
score = score + 1;
}

if (q1 != "C" && q1 != "c")
{
cout << "You didnt answered good :( \n\n";
score = score + 0;
}

string q2 = "";

cout << "q 2:\nIn which year was Mihai Eminesc born? \n\na = '1850'\n" << "b = '1905'\nc = '1889'\nd =" << " '1851'\n" <<endl;
cin >> q2;
cout << "\n" << endl;

if (q2 == "a" || q2 == "A")
{
cout << "You answered corectly! +1p\n\n";
score = score + 1;
}

if (intrebarea2 != "a" && intrebarea2 != "A")
{
cout << "You didnt answered good :( \n\n";
score = score + 0;
}
first, it is bad design to put the questions and answers apart. You do what the professor tells you, but that is just making extra work, and introduces bug problems (now you have to relate answers and questions back together, the simple way is to assume they are in the correct order in both files; if you want to make it a little safer you can put a unique ID on each question and answer to relate them).

that said, youll have to read all the questions in that file and the related answers in the other file into a data structure. Then randomly select a sub-set or ordering for the questions from your container.

so basically..
make a struct that can hold a question, its answers, and which answer is correct.
make a vector of the above.
write a method read the files to populate the vector of structs.
write a method to randomly choose from the vector using <random>

I don't know if you are not permitted to use various things and what you know so far.
struct question
{
string qtext;
vector<string> answers(10); //whatever, how many answers are allowed? 5? more?
int correct answer; //valid and correct index into above.
};
vector<question>;
etc...
Last edited on
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
#include <iostream>
#include <string>
#include <random>

int main()
{
    // COULD USE A STRUCT INSTEAD OF SEPARATE ARRAYS
    std::string question[4]
    {
        "How many sides on a triangle", "What is cheese made of?",
        "What is Presidents Trump's first name?", "What's helios' real name?"
    },
    answer[4]
    { "3", "milk", "Donald", "Carol"};
    
    std::random_device random_device;
    std::mt19937 random_engine
    { random_device() };
    
    std::uniform_int_distribution<int> st_distribution{ 0, 3 };
    
    std::string reply;
    for (int i = 0; i < 10; i++)
    {
        
        int question_no = st_distribution(random_engine);
        
        std::cout << question[question_no] << '\n';
        std::cin >> reply;
        
        if( reply == answer[question_no])
            std::cout << "Correct\n";
        else
            std::cout << "Wrong\n";
    }
    
    return 0;
}


Something to play with, perhaps if you've done include structs or even classes synchronising the questions and answers is less prone to error.
Last edited on
You will need to define the format of your input file. Here's one possibility that should be pretty easy to parse.

Q
How many neighbours does Romania have?
A
3
4
*5
6

Q
In which year was Mihai Eminesc born?
A
*1850
1905
1889
1851


Specifically:
- A Question starts with a line containing a single "Q" character.
- Following the Q are some number of lines with the text of the question.
- A line containing a single "A" character marks the end of the question and beginning of the answers.
- Each line contains a possible answer. The line starting with '*' is the correct answer.
- The answers end with a blank line.

Create a class that represents a question and its answers. Write one method to read the question from a stream and another method to print it out to the user, formatted so the possible answers have "A: ", "B: " etc before them.

Test these two methods. Create a file with one question and its answers. Make sure you can display it properly.

Add a method that prompts the user for their answer and determines if they are correct.

Now add the remaining code to read a vector of questions from the file and to ask each question in turn.
Topic archived. No new replies allowed.