Hello! I'm creating a program to input a list of words from a file into an array. From this point, I'm trying to randomly select one of the words stored within the array. To test if the code works, I have told the program to display the random word. Unfortunately, the program does not display anything and acts almost like it's skipping the entire process.
For simplicity, I have commented out the portions of unfinished code.
I am really unsure on what I did wrong exactly and would greatly appreciate if someone can turn me into the right direction.
// Constant for the maximum number of words.
constint MAX_NUMS = 100;
int main()
{
// Variables
string oneWord, randomWord, words[MAX_NUMS];
char letter;
int count = 0, position;
// Set up random generator
srand(static_cast<unsignedint>(time(0)));
// Declare an ifstream object named myFile and open a file.
ifstream myFile;
myFile.open("P4Words.txt");
myFile >> oneWord;
while (!myFile.eof())
{
words[count] = oneWord;
count++;
myFile >> oneWord;
}
cout << "There are " << count << " words in the file." << endl;
randomWord = words[rand() % count];
cout << randomWord; //To test if randomWord works or not.
The code is fine either way, the difference is that std::endl flushes the text to the console - otherwise the text just stays in the buffer and might not be shown on the screen. If you don't want the newline you can use std::flush.