+ 5
How can I random quiz questions in C++?
Have created a quiz game. I need the questions to randomly display when i open the program.
10 Answers
+ 9
Lucky Bill
Your question gives no sense.
Please explain.
+ 6
Lets say you have a string array
string arr[100];
which contains all the questions
now we generate a number which is less than maximum size
srand(time(NULL));
int random = (rand()%100);
now get the question with the random index number
cout << arr[random];
NOTE:-
<cstdlib> is needed for rand() and srand()
<ctime> is needed for time()
+ 5
What exactly do you mean? Randomize the order of certain questions or picking a random question (from an array of questions e.g.)
+ 4
If you want to choose a question randomly from let's say a vector of questions:
You'd use the function rand to generate a random number from 0 to size of your vector minus 1 and use that number for accessing the question.
cout << your_vector.at(
your_random_number)
https://en.cppreference.com/w/cpp/numeric/random/rand
+ 4
Lucky Bill You can also use
cout<<arr[(rand()%100)] ;
directly
I showed that integer for explanation
+ 4
You can add your quiz game to SL Code Playground and save it. Other users may find it useful.
+ 3
Flaming Arrow thanks.. Let me try that out..
+ 2
Aaron Eberhardt i want to make my quiz program questions to display randomly when i open the program......
+ 1
Sonic here is the code.. Try out..
https://code.sololearn.com/ceadKrLJjoWh/?ref=app
0
yes