mardi 16 avril 2019

Why does this loop only access a random line from my text file once?

I'm very much a novice and am trying to get this piece of code to work. The portion I will share is intended to take a random line from a very simple text file list. The idea is to get a random question, answer it, get another, answer it, and so on until you hit the amount selected by the user. In the case of this, 10 questions.

ifstream File("TestBank.txt");

int i = 0;

while (i < 10) {
    srand(time(NULL));
    int randomQuestion = rand() % 100;
    int questionCount = 0;

    getline(File, line);

    while (getline(File, line)) {
        ++questionCount;

        if (questionCount == randomQuestion) {
            cout << line << endl << " True or False? ";
            cin >> answer;
        }
    }

    i++;
}

While it does do as intended, it does not loop like I want it to. I get one random question, answer it, and the program ends.




Aucun commentaire:

Enregistrer un commentaire