void createVideoList(ifstream& ifile, Video videoArray[])
{
string title;
string star1;
string star2;
string producer;
string director;
string productionCo;
int inStock;
int count = 0;
Video newVideo;
getline(ifile, title);
while (ifile)
{
ifile >> inStock;
getline(ifile, title);
getline(ifile, star1);
getline(ifile, star2);
getline(ifile, producer);
getline(ifile, director);
getline(ifile, productionCo);
videoArray[count] = Video(inStock, title, star1, star2, producer, director, productionCo);
count++;
}
}
This is my code for a programming assignment. It will read in from a .txt file and is going to place the information into an array of a class I created.
The .txt is formatted like so:
3 (amount in stock)
Movie Title
Movie Star1
Movie Star2
Movie Producer
Movie Director
Movie ProductionCo
However, my code does not seem to be gathering the data correctly into the videoArray.
I just switched over from Java so my C++ syntax is a little rusty. Am I using getline correctly?
If I try to output one of the indexes, it has nothing in any of the variables. Thanks in advance!
It is mostly correct, but there are a few problems:
getline, the one outside of the loop, shouldn’t be there. What is it supposed to read?>>withgetline. The>>doesn’t read in the remainder of the first line — specifically, it leaves the\nin the input stream. Usestd::getlineoristream::ignoreto remove the pending end-of-line.std::vectorinstead of an array, if the homework assignment allows it.Try:
As a demonstration of the language features that you will learn in the coming weeks, here is one implementation of your program using modern C++ features: