Итак, у меня есть код здесь. Цель состоит в том, чтобы создать викторину с множественным выбором, где вопросы поступают из текстового файла и будут выбраны случайным образом для отображения. Я смог сделать множественный выбор, проблема в том, когда он генерируется, вопрос, который уже был выбран и отображен, отображается снова. Также выбор исходит из ответов на текстовый файл, но проблема в том, что выбор неоднократно отображается в вопросе.Allways повторяющийся случайный выбор
Так что я хочу знать, как не отображать вопрос выбора еще раз, и как выбор не отображается повторно.
Пример вывода
1. what month do we celebrate Christmas?
a.december
b.december
c.january
d.december
//Randomizes the questionList vector
random_shuffle(questionList.begin(), questionList.end());
//Goes through every Test question
for(int i = 0; i < questionList.size(); i++){
vector <Test> randomAnswer;
//Puts the correct Answer into it first
randomAnswer.push_back(questionList[i]);
//Then randomly gets 3 other answers from questionList
while(randomAnswer.size() < 4)
{
int random = rand();
if(random != i){
randomAnswer.push_back(questionList[rand() % (questionList.size() - 1)]);
}
//Shuffle the answers
random_shuffle(randomAnswer.begin(), randomAnswer.end());
//Print the question
cout << questionList[i].getQuestion() << ":" << endl;
//Initialize the first choice character to 'A'
char ch = 'A';
//Prints the shuffled answers
for(int j = 0; j < randomAnswer.size(); j++)
{
cout << ch << ") " << randomAnswer[j].getAnswer() << endl;
//Increment 'A' so it can print 'B' and so forth
++ch;
}
//Get users response
cout << "\nYour answer: ";
cin >> response;
//Bool data type to determine if the correct answer was found
bool isCorrect = false;
switch(toupper(response))
{
case 'A':
if(randomAnswer[0].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'B':
if(randomAnswer[1].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'C':
if(randomAnswer[2].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
case 'D':
if(randomAnswer[3].getAnswer()==questionList[i].getAnswer())
isCorrect = true;
break;
default:
cout << "\nIncorrect input.\n";
}
//If the answer was found print "Correct" else "Wrong"
if(isCorrect)
{
cout << "\nYou got the answer correct!\n";
}
else
{
cout << "\nYou got the answer WRONG!\n"
<< "Correct answer was " << questionList[i].getAnswer() <<
endl;
Эта перегрузка 'std :: random_shuffle' скоро будет устаревшей. Лучше использовать 'std :: shuffle'. – chris