Hello all,

It has been a while but I'm on the right path - finally.

So basically I am writing a very special application for a loved one. I have many great ideas but simply put I am struggling with I/O stream, it is very possible that I should research this subject more but none the less I am asking here.

My application will expect a series of questions within a text file, my program should be able to read these randomly (Heres the catch..) but only once! this is where I am struggling. Perhaps I am approaching it all wrong, my program is looking for the user to score 10, each question within the text file will be worth 1 point, I want to avoid repeat questions as there will only be around 15 questions.

I would appreciate any advice and/or examples you can give, many thanks in advance..

Recommended Answers

All 6 Replies

Assign a number to each question in the text file. Then, generate a random number and, for each number generated, pull the corresponding line of the text file.

I'm not a C++ programmer, but some languages have libraries that let you pull n numbers at once. If C++ isn't one of those, then create an array of size 15. Each time you generate a random number, see if it already exists in the array, and if it does, go through a while loop pulling new numbers until you find one that hasn't already been used, and then put it in the array, and pull the corresponding line from the text file.

The C++ STL (Standard Template Library) has a lot of collection types which will work well for this including vectors (resizable arrays), maps, hashmaps, etc. Go to http://www.cplusplus.com/reference/ for the entire reference manual.

The way I would do this is to read in all of the questions into an array. After that then shuffle the array around. Here is a little example

std::string questions[15];
// load the questions into questions[]

srand(time(NULL));  // this seeds rand
int randIndex;

for (int i = 0; i < 15; i++)
    randIndex = rand() % 15;  // gets a random number from 0 to 14
    std::swap(question[i], question[randIndex]);

The way I would do this is to read in all of the questions into an array. After that then shuffle the array around.

Yes, that makes sense, since you're only reading from the file once. Although I'm not really a C++ person. If you were to open the file for reading, then would it put the entire thing into RAM, and you could selectively read individual lines just as fast as reading from an array? In such a case, wouldn't it actually be more overhead to copy the entire file into an array instead of just reading from the already opened file?

From what I have experianced reading or writing to the file is slower than reading or writing to an array. The OS might load the file into RAM but I dont think streams in c++ don't.

The OS will cache the file (at least Linux will), but if the stream is using unbuffered I/O, then it will not pass that to the process. It will usually physically read every time you access the file to read a line of data.

Be a part of the DaniWeb community

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