I have a textfile that I'm trying to copy each word in the file to an individual element in an array, then output the contents of the array to the screen.
My code will copy an entire line, but not an individual word. Or it will just copy the first letter of the first word. My loop doesn't seem to run either.
Could someone point me in the right direction? I am thinking it's an error with while (!inputFile.EOF) statemetn... Also, I can only use cstrings, I cannot use strings.
Here's my code and contents of textfile are below:
Contents of text file: Rockies Angels Dodgers Astros
1 2 3 4 5 6 7 8 9 10 11 12
ifstream inputFile;
inputFile.open("testfile.txt");
inputFile.getline(testArray, 25); // This copies entire line over, need to figure out how to get just word]
inputFile >> testArray[0];
cout << "Checking to see if " << testArray[0] << " copied over." << endl;
// All that is copying over is first letter of first word
while ((!inputFile.EOF)
{
inputFile >> testArray[i];
i++;
cout << "Test word: " << testArray[i];
}
So I edited my code and now I'm getting each letter of each word to copy over, and the output is:
Test word: r
Test word: o
Test word: c
Test word: k
Test word: i
Test word: e
Test word: s
and doing this for each word. I don't know how I can get the word to get copied into each element.