For some reason the full lines from my input file are not reading into the array, only the first word in each line. I am currently using the getline call, but I am not sure why it is not working. Here is the what I have for the call to populate the array. The txt file is a list of songs.
const int numTracks = 25;
string tracks[numTracks];
int count = 0, results;
string track, END;
cout << "Reading SetList.txt into array" << endl;
ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks && inputFile >> tracks[count])
{
count++;
getline(inputFile, track);
}
inputFile.close();
The
>>
operator reads a single word. And this code reads this single word into the vector in question.True, you're using
getline()
. To read the rest of the line, after the initial word, into some unrelated variable calledtrack
.track
appears to be a very boredstd::string
that, apparently, gets overwritten on every iteration of the loop, and is otherwise completely ignored.Your loop is using the
operator>>
to read the file into the array. That operator reads one word at a time. You need to remove that operator completely and usestd::getline()
to fill the array, eg:Or:
Alternatively, consider using a
std::vector
instead of a fixed array, then you can usestd::istream_iterator
andstd::back_inserter
to get rid of the manual loop completely: