for instance, I have this test.txt containing :
apple
computer
glass
mouse
blue
ground
then, I want to retrieve one random line from the text file.
here’s my code :
ifstream file;
file.open("test.txt", ios::in);
char word[21];
int line = rand()%6 + 1;
for (int x = 0 ; x < line ; x++)
test.getline (word, 21);
cout << word;
the problem is the variable ‘word’ always contains the first line, no matter what random number given…
Seed the random number as suggested by the comments above