newbie help: Input Output Text file question

C

ComicCaper

Hi all,

I'm trying to write a program that will pull 3 random lines from a list
of sentences. I have been doing some looking and found that I can
randomize the character at which the program will pull from in the text
file, but is there a way to simply randomize the line number and so I
can pull the whole line?

Thanks,

Matt
 
P

Peter Jansson

ComicCaper said:
Hi all,

I'm trying to write a program that will pull 3 random lines from a list
of sentences. I have been doing some looking and found that I can
randomize the character at which the program will pull from in the text
file, but is there a way to simply randomize the line number and so I
can pull the whole line?

Thanks,

Matt

Hi,

Yes. A suggestion follows:

1. Loop through the whole fie to find out the number of lines in it.
2. Generate three random numbers between 1 and the number of lines in
the file.
3. Open the file once more and read the three lines corresponding to the
three random numbers.

Hint: A line in the file may be read using std::getline.


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
C

ComicCaper

Peter,

Thanks for the hint. It's nice to know that there is a simple way to do
this. With my limited knowledge you don't EVEN want to know how I was
thinking I had to do it. (Lots of sorting and comparing, like trying to
get from here to there with right turns only.)

-Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top