I have made a quiz with 10 questions [I haven't posted the code since it is very long, but I will do so if it is needed]
The program gives the user a question, asks them for the correct option for the answer, and gives an output depending on the answer[if correct, a congratulation and 1 addition to the score. otherwise, the correct answer]
It also keeps a track of the total time taken be the user.
But now to the thing I wanted to know about:
I wanted to know if there was a way to make the quiz more Random (i.e. I put up, say, 20 questions and it will pick out any 10 of those question and present them to the user).
Now I wanted to know how can this be done (if it can be done at all)
Sure. You should create a question struct that would hold the question string, the option strings and an integer that shows which option is correct. Then write a function void ask(Question q); which would basically be the code you posted. Then create an array "list" of Questions and lastly, call ask(list[rand()%20]) in a loop. (To prevent questions from repeating you may want to shuffle the array instead)
Could you please explain it a bit a more elaborately? I am new to C++ coding and have learnt everything I know about it online.
The parts I couldn't understand are:
You should create a question struct that would hold the question string, the option strings and an integer that shows which option is correct.
As for making the function: How will I put in what the Questions and options are?
I didn't fully understand the tutorial on Functions. So the problem was that I don't know how to use a Function, I understood how to make one. So I was asking a bit of help on how do I use a function to put up the values I need
The correct answer is the 0 at the end. 0 is option 1, 1 is option 2, 2 is option 3, 3 is option 4.
list[#] where # is the question you want, but you can make a loop to ask the questions in order. Or, as you wanted before, you can use a randomly generated number.
O.K. I understood the option part.
Now if I try to use: ask(list[rand()%20])
as you suggested before, I get a dialogue box saying:
Unhandled exception at 0x626e1f68 (msvcp100d.dll) in Porject - Quiz - Updated.exe: 0xC0000005: Access violation reading location 0xcccccccc.
and it opens a read only code named iosfwd with a extremely long code that I cant understand one bit.
I am encountering another problem:
The program is repreaing the same question if I use in like this:
1 2 3 4 5 6 7 8 9 10 11 12
//Question 1
{
{"Coral reefs in India can be found in___", "The coast of Orissa", "Waltair", "Rameshwaram", "Trivandrum", 2}
};
ask( list[0] );
//Question 2
{
{"In which decade was the first solid state integrated circuit demonstrated?", "1950s", "1960s", "1970s", "1980s", 0}
};
ask( list[0] );
//...
It works perfectly now. And yeah, I didn't have 20 questions, I had 15.
Now I have another problem:
I used the code 15 times for the 15 questions. Now how do I prevent it from repeating the questions that have appeared before?
Out of the 15 questions, 4 were redundant in my case.
For this you had said:
To prevent questions from repeating you may want to shuffle the array instead
So how can I shuffle the array?
And I noticed that the random values were in the same order every time I used it. Any way to make the order random as well?
Again, Thanks. I understood how to use srand, Now the random numbers are truly random.
But I didn't understand the Random Shuffle. Could you please explain it?