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";

It's possible to "catch" a segmentation fault with a signal handler, but you can't resume the program after the handler runs, so it's not very useful other than terminating gracefully. In your case the better option is to fix the bug that caused the fault in the first place. :icon_rolleyes:

Oops, I missed that. You need to check whether line_counter is zero in your loop conditional, otherwise it will try to read an non-existent line, causing the segfault.

while(choice != 'q' && line_counter > 0)

Oops, I missed that. You need to check whether line_counter is zero in your loop conditional, otherwise it will try to read an non-existent line, causing the segfault.

while(choice != 'q' && line_counter > 0)

:) hi! i already added the codes... but still, it displays the segmentation fault..:?:
here's the new while loop...

while(choice != 'q' && line_counter > 0)
        {
            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;
	
		if (line_counter <= 0) 		
		{
			cout << "no more lines to show!" << endl;
		}

        }

i want to display no more lines to show! after it displayed all the 8 random lines... can you check the codes above?

I've gone over the segfault problem, and tracked it down to where you initialize line_counter to 1; you want to initialize it to zero, instead. As it is now, it over-counts the lines by one.

I've gone over the segfault problem, and tracked it down to where you initialize line_counter to 1; you want to initialize it to zero, instead. As it is now, it over-counts the lines by one.

wow! its working now! thanks a lot! you're a hero!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.