Input with files Part 2

I was reading this earlier http://www.cplusplus.com/doc/tutorial/files/
and i was trying to figure out how to pick one of the words randomly from my text instead of using all the words in it.


[/code]
Last edited on
(!qFile.eof()) Do not use eof() with loop conditions. You almost always will run into error.
Read all words into container, then choose random word:
1
2
3
4
5
6
7
8
9
10
11
#include <ctime>
#include <cstdlib>
//...
std::strand(std::time(0));
//...
std::vector<string> words;
while(qFile >> qWord) {
    words.push_back(qWord);
}
unsigned index = std::rand() % words.size();
std::cout << words[index];
Last edited on
so i load my words from a file (using std::ifstream) onto a

[/code]
Last edited on
remove line 35.
Thanks alot! works fine
Topic archived. No new replies allowed.