Hi. Can somebody help me? I am trying to create a program that reads random line from a file and displays them.
My problem is, it displays the same line every time I press ‘n’ (it suppose to display another line from a file). Can somebody tell me how to display them without repeating the same line? Thank you!
This is the function I made so far…
int easy()
{
string quest;
string randLine;
int nLines = 0;
char choice;
ifstream myfile ("./questions/easy.txt");
cout << "Welcome to EASY level category!\n" << endl;
if (myfile.is_open())
{
while(choice != 'q')
{
while( getline( myfile, quest ) )
if( ( rand() % ++nLines ) == 0 )
randLine = quest ;
cout << randLine;
cout << "\n\nPress [n] to proceed and [q] to quit: ";
cin >> choice;
}
myfile.close();
}
else cout << "Unable to open file";
}