I’m creating a program that displays random lines from a file. I have 8 lines on my text file and after displaying the last/8th random line… it displays Segmentation fault and then terminates the program.
The program should display a message saying that there are no lines left to display.
Can someone help me with this? Or can somebody tell me how to display the 8 lines only?
This is the function I made so far…
int easy()
{
vector<string> text_file;
string quest;
char choice;
int line_counter = 1;
srand(time(NULL));
ifstream myfile ("./questions/easy.txt");
cout << "Welcome to EASY level category!\n" << endl;
if (myfile.is_open())
{
while( getline( myfile, quest ) )
{
text_file.push_back( quest );
line_counter++;
}
while(choice != 'q')
{
int line = rand() % line_counter;
cout << text_file[line];
text_file.erase(text_file.begin() + line);
line_counter--;
cout << "\n\nPress [n] to proceed and [q] to quit: ";
cin >> choice;
}
myfile.close();
}
else cout << "Unable to open file";