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

one thing is ye' need to call srand() somewhere in order to seed ye' random number generator.

I would load your entire text file into a data structure, line by line, and then get lines out of the data structure at random.

vector<string> text_file;
int line_counter = 1;
string line;
srand(time(NULL));

//Load the text file
while(getline(infile, line))
{
     text_file.push_back(line);
     line_counter++;
}

//Get a random line
cout << text_file[rand()%line_counter];

one thing is ye' need to call srand() somewhere in order to seed ye' random number generator.

I would load your entire text file into a data structure, line by line, and then get lines out of the data structure at random.

vector<string> text_file;
int line_counter = 1;
string line;
srand(time(NULL));

//Load the text file
while(getline(infile, line))
{
     text_file.push_back(line);
     line_counter++;
}

//Get a random line
cout << text_file[rand()%line_counter];

I did what you suggested Mr. Clinton. It now displays random lines. But still, it keeps repeating the same line after a few tries. I have a total of 8 lines on my text file, and after my 4th try, it displayed the first random line.
This is the modified function. I added the codes that you suggested.

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(choice != 'q')
		{
    		while( getline( myfile, quest ) )
			{		
	   			text_file.push_back( quest );
				line_counter++;
			}

			cout << text_file[rand()%line_counter];
			cout << "\n\nPress [n] to proceed and [q] to quit: ";
			cin >> choice;
		}
	
    	myfile.close();
  	}
  		
	else cout << "Unable to open file"; 
}

Do you have any suggestions?

I would recommend two changes. First, move the input loop to before the output loop, such that you collect all of the lines before you start outputting the randomly selected lines. Second, after displaying a line, remove that given line from the vector using the single argument version of the erase() method.

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

I would recommend two changes. First, move the input loop to before the output loop, such that you collect all of the lines before you start outputting the randomly selected lines. Second, after displaying a line, remove that given line from the vector using the single argument version of the erase() method.

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 working now!! thanks a lot!! :)

I would recommend two changes. First, move the input loop to before the output loop, such that you collect all of the lines before you start outputting the randomly selected lines. Second, after displaying a line, remove that given line from the vector using the single argument version of the erase() method.

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

Hi! i have a following question..

after displaying all the 8 random lines... it displays segmentation fault how can i solved this? I want to display a message saying that there are no more lines to display.. where exactly will I put this?

and BTW i'm using the codes that Schoil-R-LEA posted earlier...

help!! please!!? :confused:

i am making a quiz manegment program so how can i read and write random questions
which are in text file.

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.