I”m trying to write a program that allow a user to track their DVDs. Use an input file which include these attribute for each movies.
Basically, the user should be able to enter in the name of input file.
Using a structs and pointers, created a linked list of the movies in the input file.
Next, output the linked list to an output file.
Format the output of the plot such that it will word wrap.
The output suppose to look like this
***************************************************************************
MOVIE #: 1 Title: Antwone Fisher
---------------------------------------------------------------------------
Year: 2002 Rating: 7
---------------------------------------------------------------------------
Leading Actor: Denzel Washington Genre 1: Biography
Supporting Actor: Derek Luke Genre 2: Drama
---------------------------------------------------------------------------
PLOT:
Antwone Fisher, a young navy man, is forced to see a psychiatrist after a
violent outburst against a fellow crewman. During the course of treatment
a painful past is revealed and a new hope begins.
***************************************************************************
So far, the problem is my output and I can’t display the output. I try to debug it and it turn out that I’m stuck on the while loop.
Here’s my output function
void OutputList(MovieDVD *head)
{
ofstream OFile;
OFile.open("OFile.txt");
MovieDVD *dvdPtr;
dvdPtr = head;
cout << "Before While Output " << endl;
while(dvdPtr != NULL)
{
OFile << left;
OFile << dvdPtr -> title;
OFile << dvdPtr -> leadActor;
OFile << dvdPtr -> supportActor;
OFile << setfill('*') << setw(25) << '*' << endl;
OFile << dvdPtr -> genre;
OFile << setfill('*') << setw(25) << '*' << endl;
OFile << dvdPtr -> altGenre;
OFile << setfill('*') << setw(25) << '*' << endl;
OFile << dvdPtr -> year;
OFile << setfill('*') << setw(25) << '*' << endl;
OFile << dvdPtr -> rating;
OFile << setfill('*') << setw(25) << '*' << endl;
OFile << dvdPtr -> synopsis;
OFile << setfill('*') << setw(25) << '*' << endl;
dvdPtr = dvdPtr -> next;
cout << endl;
cout << "READ - AFTER LINE OUTPUT" << endl;
}
cout << " DEBUG -- AFTER WHILE OUTPUT" << endl;
delete dvdPtr;
dvdPtr = NULL;
OFile.close();
}
Here my main function:
//INPUT OUTPUT FILE DECLARATION
ifstream inFile; //INPUT FILE declaration
ofstream OFile; //OUTPUT FILE declaration
//VARIABLE DECLARATION
string inputFileName; //IN - Input file name
string outputFileName; //OUT - Output file name
string synopsis;
MovieDVD *head;
MovieDVD theMovie;
head = NULL;
//INPUT
cout << "What input file would you like to use?: ";
getline(cin, inputFileName);
cout << "DEBUG -- BEFORE CREATELIST "<< endl;
CreateList(head, inputFileName);
cout << "DEBUG -- AFTER CREAETLIST " << endl;
//OUTPUT LIST
cout << "What output file would you like to use?: ";
getline(cin, outputFileName);
OutputList(head);
WordWrap(OFile, head, synopsis);
I add the cout for my debug on output list and only
cout << "Before While Output " << endl and cout << " DEBUG -- AFTER WHILE OUTPUT" << endl come out and skip the while loop. Help me please.
My Creative List Function
void CreateList(MovieDVD *head, string inputFileName)
{
ifstream inFile;
inFile.open("inFile.txt");
MovieDVD *dvdPtr;
head = NULL;
dvdPtr = NULL;
dvdPtr = new MovieDVD;
int counter =0;
cout << " DEBUG -- BEFORE WHILE" << endl;
while (inFile && (dvdPtr != NULL))
{
getline(inFile, dvdPtr -> title);
getline(inFile, dvdPtr -> leadActor);
getline(inFile, dvdPtr -> supportActor);
getline(inFile, dvdPtr -> genre);
getline(inFile, dvdPtr -> altGenre);
inFile >> dvdPtr -> year;
inFile >> dvdPtr -> rating;
inFile.ignore(1000, '\n');
getline(inFile, dvdPtr -> synopsis);
dvdPtr -> next = head;
head = dvdPtr;
dvdPtr = new MovieDVD;
cout << "READ line " << counter++ << endl;
}
cout << " DEBUG -- AFTER WHILE" << endl;
delete dvdPtr;
dvdPtr = NULL;
inFile.close();
}
You are not creating the list in your
CreateListfunction. Well, you are, but not in a way you would like to. See, your list is being created, and the pointerdvdPtrpoints to it. So far, so good. But then you do this:You assume that after the function exits, the
headpointer would hold the list, but it’s wrong. In this line you are changing a local pointer, so once the function exits, you lose it. That’s probably the reason why your output skips thewhileloop – the list is just empty. You need to pass a reference to your pointer:then it will allow you to change it the way you want.