For class I have a project where I am given a file and the first line is a number that indicates how many structures I will need to make to house the data, and then it starts with that many 3-part sections that I need to read into a structure. The form goes Title of Movie, Rating, and Profit. I.E.:
1
The Pirates of the Caribbean
8.3
382938.00
The problem I am having is when I try to read in the title of the movie, I output it right after I read it in to make sure it is reading correctly, but I am getting nothing. It reads in blank. The same goes for reading in the next two lines, they print out as 0.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
for (int count = 0; count < numberOfMovies; count++)
{
getline(inputFile, titleTemp);
cout << titleTemp << endl; //Prints out as a space
inputFile >> ratingTemp;
cout << ratingTemp << endl; //Prints out '0'
inputFile >> profitTemp;
cout << profitTemp << endl; //Prints out '0'
moviePointer[count].title = titleTemp;
moviePointer[count].rating = ratingTemp;
moviePointer[count].profit = profitTemp;
}
Not sure, but this doesn't appear to be the proper way to read from a file, I would say that you should be using a buffer to read the data from the file instead of dumping your stream object into the cout stream.
The way I was usually taught, for numbers for instance, is:
inputfile >> myVariable;
and if you put that in a loop it keeps going down the line reading them in. You could read it into an array as well, for example. That only works for numbers though.