Random string selector

I would like to create a trivia game and i need a code that chooses random questions.. i have no idea how to do that, can somebody tell me an idea of how i can implement that?
make an object of questions with their answers:
struct qna
{
string question;
string answer;
};
make a container of those:
vector<qna> quiz(20); //heh, 20 questions...
and randomly select one of those:
int index = <random> stuff goes here (see the tutorial on random) that generates 0-19 uniform distribution
then cout << quiz[index].question;
and proceed like that.

you will see the idea of getting a random item from a container over and over in some careers, so this is something you need to understand and remember.
you can also randomly order the vector, and then go through it in order, eg you want 5 questions, then [0] through [4] are fine and ensure no repeats (random index may repeat!).
https://www.cplusplus.com/reference/algorithm/random_shuffle/ can do it that way.
Last edited on
Hi Drayt,
I often use rand() function in stdlib.h to get random numbers.It can get a random number between 0 and 32767.rand() needs a seed to get random numbers,and the seed is acquiescently 0.If we don't set the seed,the random numbers will be always the same.So,we can use srand() to set the seed.To make the random numbers different,the seed can be time(0) in time.h,which can return how many seconds are there between Jan 1st,1970 to the time now.Then I often Multiply it by 3 to make a bigger difference.And,define the questions and the answers.If you don't understand,just look at the following codes and try to understand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>    //input and output
#include<stdlib.h>     //rand() and srand()
#include<time.h>      //time()
#include<string.h>    //use strcmp() to compare two strings
using namespace std;
int main(){
    char* questions[4]={"Question #1","Question #2","Question #3","Question #4"};
    char* answers[4]={"#1","#2","#3","#4"};
    srand(time(0)*3);   //set random number seed
    int choice=rand()%4;   //a random number between 0 and 3,actually
    cout<<"Now I'm gonna ask you a question!\n";
    cout<<questions[choice];   //output the random question
    cout<<"\nPlease input your answer:";
    char input[100];
    cin>>input;
    if(strcmp(input,answers[choice])==0)  //0 means they are the same
        cout<<"Oh my god!You answered it correctly.\n";
    else
        cout<<"Haha!That's not correct.The correct answer is:"<<answers[choice]<<".\n";
    system("pause"); //pause
    return 0;
}

I hope those can help you:)
P.S. My English isn't good so try to fix my grammatical errors,thanks X)
Last edited on
Use c++ random rather than the c rand() et al. Rather than using c-style null-terminated char for input, use c++ string. If you use c-style null-terminated char* pointers for string constants, the these should be const.

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
#include <random>
#include <iostream>
#include <string>
#include <iterator>

int main() {
	const char* const questions[] {"Question #1", "Question #2", "Question #3" ,"Question #4"};
	const char* const answers[] {"#1" ,"#2" ,"#3" ,"#4"};

	static_assert(std::size(questions) == std::size(answers));

	std::mt19937 rng(std::random_device {}());
	std::uniform_int_distribution<size_t> distrib(0, std::size(questions) - 1);

	const auto choice {distrib(rng)};

	std::cout << "Now I'm gonna ask you a question!\n";
	std::cout << questions[choice];

	std::string input;
	std::cout << "\nPlease input your answer: ";
	std::getline(std::cin, input);

	if (input == answers[choice])
		std::cout << "Oh my god! You answered it correctly.\n";
	else
		std::cout << "Haha! That's not correct.\nThe correct answer is: " << answers[choice] << '\n';
}

Topic archived. No new replies allowed.