samedi 7 octobre 2023

How do I flag duplicates in my random question/answer generator loop?

I'm working on a program that's supposed to be like a trivia quiz. It picks a random question and answer from a list of strings for each. But I'm not sure how to prevent it from displaying that same question/answer again after it's already been displayed. I'm guessing I'd have to flag it, but don't know how to implement that.

Here's a snippet of the question generator:

srand(time(0));
    questionGenerated = rand() % 6 + 1; // picks a num 1-6 and looks for matching case 
    switch (questionGenerated)          // containing the question and correct answer strings
{
    case 1:
        cout << question1String;
                correctAnswer = answer1String;
                break;
// ...and so on with cases 2-6
}

I also made a thing to display the answers next to numbers in ascending order, so it can be a menu you can pick from while still having the answers randomized. Having the same issue.

// find out the correct answer int first,
// so incorrect answer int can be compared to it:

correctAnswerInt = rand() % 3 + 1;

    while (reroll > 0)
    {       // pick the number the incorrect answer will go next to:

        incorrectAnswerInt = rand() % 3 + 1;

                // determine where it and correct answer will go in the ascending order:

        if (incorrectAnswerInt > correctAnswerInt)
        {
            cout << correctAnswerInt << " " << correctAnswer << "\n";
            reroll -= 1;
        }
        else if (incorrectAnswerInt < correctAnswerInt)
        {
            cout << incorrectAnswerInt << " " << incorrectAnswer << "\n";
            reroll -= 1;
        }
                // or if it's equal to correct answer int, roll again:

        else
            reroll += 1;
    }

Example of intended output, for reference:

Question 3

1 Incorrect Answer
2 Correct Answer
3 Incorrect Answer

It's not working as intended right now but I'm mainly worried about flagging duplicates. Like, not getting 2 or "What is the capital of Belgium?" twice in a row.

I thought about adding an if/else with a bool that if true, rerolls the question. My fear is that something like this would cause an infinite loop:

while (questionHasBeenDisplayed = true)
{
       switch (questionGenerated)
       {
       case 1:
               questionHasBeenDisplayed = true;
           if (questionHasBeenDisplayed)
           // not sure how to send it back to start of loop from here,
               // but imagine some code here that does that
    else
            cout << question1String;
            correctAnswer = answer1String;
       // etc.
       }
}

I could also try adding a flag for each question, but that seems like a lot of variables.

For my answer code, the only thing I can think of is this:

while (reroll > 0)
{
    incorrectAnswerInt = rand() % 3 + 1;
    if (incorrectAnswerInt == incorrectAnswerInt && incorrectAnswerInt > correctAnswerInt)
// etc...
}

Which would always fail and cause an infinite loop.

I'm wondering now if there's a way to check the previous value of the variable (from the last loop) and compare it to the current value of the variable in the loop. Which would probably just mean making another variable, again, and reworking the code.

I'll guess I'll look into that now, but in the meantime any advice is appreciated. Thanks.




Aucun commentaire:

Enregistrer un commentaire