Randomizing a Quiz

Pages: 12
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)

P.S. The Questions follow this format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << Question;
cout << option1;
cout << option2;
cout << option3;
cout << option4;

cin << answer variable;
if (answer variable == answer)
  { 
    socre +=1
    cout << congratulation quote;
  }
else
  { 
   cout << correct answer;
  }

Thanks in advance
Last edited on
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?

And which type should I use for the Array "list"
1
2
3
4
struct Question{
   std::string question, option[4];//you may use const char* if you want
   int correct;
};


1
2
3
4
Question list[] = {
   {"q1", "o1", "o2", "o3", "o4", 2}, //you may want to write constructor
   ...
};
O.K.
Now how do I use a function in the code?

1
2
3
4
Question list[] = {
   {"q1", "o1", "o2", "o3", "o4", 2}, //you may want to write constructor
   ...
};



What is a constructor?
What is a constructor?
http://www.cplusplus.com/doc/tutorial/classes/ scroll down to "Constructors and destructors"

Now how do I use a function in the code?
Try ask(list[0]); ? What exactly is the problem?
Last edited on
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
Here's how your code could look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//includes..

struct Question{
   std::string question, option[4];
   int correct;
};

void ask( Question q ){
   // you know what to write here
}

int main(){
   Question list[] = {
      {"What's a banana?", "fruit", "vegetable", "dog", "programing language", 0}
   };
   ask( list[0] );
   return 0;
}
1
2
void ask( Question q ){
   // you know what to write here 


Do I have to write this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << Question;
cout << option1;
cout << option2;
cout << option3;
cout << option4;

cin << answer variable;
if (answer variable == answer)
  { 
    socre +=1
    cout << congratulation quote;
  }
else
  { 
   cout << correct answer;
  }


And I will use the function for every question in the quiz?
yes
1
2
3
void ask( Question q ) {
   // you know what to write here
}


For efficiency purposes, you may want to pass in by reference indeed.

e.g
void ask( Question& q ) {
// you know what to write here
}
When I use this in this code:
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
40
41
42
43
44
45
46
47
48
49
50

//Arrayrs, Variables and Strings
	int Q[10];
	float score = 0;
	int opt1;
	int opt2;
	int opt3;
	int opt4;
	char que;
	int answer;
	string correct = "This is the correct answer";
	string wrong = "Sorry, this is the incorrect answer. The correct answer was ";   /*The correct option will have to be put after "<<"*/
	string skip = "You skipped the Question";
	string divider = "--------------------------------------------------------------------------------";

//Struct
struct Question{
   string question, option[4];
   int correct;
};

//Function
void ask( Question q ){
	cout << que;
	cout << opt1;
	cout << opt2;
	cout << opt3;
	cout << opt4;

cin >> answer;
if (answer == answer)
  { 
    score +=1;
    cout << correct;
  }
else
  { 
    score -= 0.5;
  }
}
//Timer and instructions...

//Question 1
	Question list[] = 
	{
		 {"What's a banana?", "fruit", "vegetable", "dog", "programing language", 0}
	};
ask( list[0] );

return 0;

The output I get is:


The Instructions...
---
 0000_


And if I enter any value, the result is always correct.
So how will I define the correct answer?
And what is the problem in this code?
Last edited on
When I said "you know what to write here", I didn't mean that you should copy your old code.
1
2
3
4
5
6
7
8
void ask( const Question& q ){//as sohguanh pointed out
   std::cout << q.question << '\n';
   for(int i = 0; i < 4; i++) std::cout << i+1 << ". " << q.option[i] << '\n';
   int choice;
   std::cin >> choice;
   if( q.correct+1 == choice ) std::cout << "yay";
   else std::cout << "no, it was " << q.option[q.correct];
}
Ah! It works fine now. No which part of this code...

1
2
3
4
Question list[] = {
      {"What's a banana?", "fruit", "vegetable", "dog", "programing language", 0}
   };
   ask( list[0] );


...denotes the correct answer?
And Do I have to change the list[0] to list[1] for every question?
Last edited on
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] );
//... 

What should I do?
Last edited on
That 20 means the number of questions in your list. I bet you didn't have 20.

The code of your another problem is messed up.
1
2
3
Question list[] = {
   {"What's a banana?", "fruit", "vegetable", "dog", "programing language", 0}
};
Is an array declaration (and initialization).
1
2
3
{
	{"Coral reefs in India can be found in___", "The coast of Orissa", "Waltair", "Rameshwaram", "Trivandrum", 2}
};
Is nothing (literally. It generates no code)
If you want to have several questions,
1
2
3
4
5
6
Question list[] = {
   {"q1", "o", "o", "o", "o", 0},
   {"q2", "o", "o", "o", "o", 0},
   //...
   {"qn", "o", "o", "o", "o", 0}
};
Is the right way.
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?
Pages: 12