Randomizing a Quiz

Pages: 12
1
2
some_type array[50] = { /*...*/ };
std::random_shuffle( array, array+50 );

Iterators act a lot like pointers so if a function asks for iterators, if you are dealing with an array, just give it pointers to the first and (one after) last elements.
I will try this. I have added a scoring feature into the quiz. It adds 1 mark for correct, deducts 0.5 for incorrect and does nothing for skipping the question[at least in the previous version there was an option to skip]. I tried to add the condition that if the user inputs 0, the program would give the output
You skipped the question

and continue to the next question. But it gives e an error message when I try to skip a question.
I modified the function to:
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
void ask( const Question& q )
{
   cout << q.question << endl;
   for(int i = 0; i < 4; i++) 
	   cout << i+1 << ". " << q.option[i] << endl;
   int choice;
   cout << "Ans.";

   cin >> choice;

 if( q.correct+1 == choice ) 
{
	 cout << correct <<endl <<endl;
	 score = score + 1;
	 cout << divider <<endl ;
}
else if (choice == 0)
{
              cout << skip;
              cout << divider <endl;
}
else 
{
               cout << wrong << q.option[q.correct] <<endl <<endl;
               score = score-0.5;
	 cout << divider <<endl ;
}
}


Here, correct, wrong, divider and skip are strings made by me and I have checked that they are working.
The error is similar to the one I gave when I tried to use the rand incorrectly.
Any help?
line 20: you're missing one <
line 20: you're missing one <

Thanks, it worked, but strangely, it didn't prompt me about this error while compiling, as is always did.
1
2
some_type array[50] = { /*...*/ };
std::random_shuffle( array, array+50 );


What should I type in array[50] = {/**/}

Is the list in the program the array? that I must use, or do I have to create another one?

Another thing: I had used Single Quotation Marks['x'] in the questions, but the first quotation mark is the closing quotation mark [pointing to the down and left]. Any way to correct this?
Last edited on
Yes, array is list and no, you don't need to copy anything.
But what about the quotation marks? If I put something in quotation marks like
'tree'
, what I get is
’tree’

Any way to solve that?

And i shuffled the array. But the problem persists. Say I put 50 questions and call for 40 of them. I am getting some of the questions twice, and sometimes even thrice.

Any way to get rid of the redundancy?
Last edited on
Any way to solve that?
I suppose such is your command line font?

Any way to get rid of the redundancy?
Post your code. Might it be that you still use ask(list[rand()%n]); ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Question list[] = 
{
/*1*/{/*the first question*/}
....
/*75*/ {/*the last question*/}
};

//Randomizing the questions
time ( &seconds );
srand (seconds);
random_shuffle( list, list+75 );

//Calling the Questions
/*1*/  ask( list[rand()%75] );
/*2*/  ask( list[rand()%75] );
/*3*/  ask( list[rand()%75] );
/*4*/  ask( list[rand()%75] );
/*5*/  ask( list[rand()%75] );
/*6*/  ask( list[rand()%75] );
/*7*/  ask( list[rand()%75] );
/*8*/  ask( list[rand()%75] );
/*9*/  ask( list[rand()%75] );
/*10*/ ask( list[rand()%75] );


By the way, only want any 10 [or any other number] of questions to be printed in the program, so simply shuffling wouldn't work. And even if I did that, the quiz would become too long.
after you have shuffled, no need to randomize anything else. Just ask questions 0-9 (using a for loop).
Of course! I didn't think of that. thanks a lot.
Topic archived. No new replies allowed.
Pages: 12