1

I'm very new to pointers and linked lists but I'm trying to write a simple program that reads in data from a text file into a linked list. I'm having trouble with the input function. It looks like this:

DVDNode* CreateList(string fileName)
{
    ifstream inFile;
    inFile.open(fileName.c_str());

    DVDNode* head = NULL;
    DVDNode* dvdPtr;
    dvdPtr = new DVDNode;

    while(inFile && dvdPtr != NULL)
    {
        getline(inFile, dvdPtr -> title);
        getline(inFile, dvdPtr -> leadActor);
        getline(inFile, dvdPtr -> supportingActor);
        getline(inFile, dvdPtr -> genre);
        cin >> dvdPtr -> year;
        cin >> dvdPtr -> rating;
        getline(inFile, dvdPtr -> synopsis);

        dvdPtr -> next = head;
        head = dvdPtr;
        dvdPtr = new DVDNode;
    }

    delete dvdPtr;
    inFile.close();

    return head;
}

I also have an output function that looks like this:

void OutputList(DVDNode* head, string outputFile)
{
    ofstream outFile;
    outFile.open(outputFile.c_str());

    DVDNode* dvdPtr;
    dvdPtr = head;

    while(outFile && dvdPtr != NULL)
    {
        outFile << dvdPtr -> title;
        outFile << dvdPtr -> leadActor;
        outFile << dvdPtr -> supportingActor;
        outFile << dvdPtr -> genre;
        outFile << dvdPtr -> year;
        outFile << dvdPtr -> rating;
        outFile << dvdPtr -> synopsis;

        dvdPtr = dvdPtr -> next;
    }

    outFile.close();

}

Here's what the main code looks like:

// Variables
string inputFile;
string outputFile;
DVDNode* head;

// Input
cout << "Enter the name of the input file: ";
getline(cin, inputFile);

head = CreateList(inputFile);

// Output
cout << "Enter the name of the output file: ";
getline(cin, outputFile);

OutputList(head, outputFile);

I'm sorry if this is a stupid question, I can't find any good tutorials online about linked lists and I don't really understand why this just doesn't do anything after I type in the Input File Name.

Thanks in advance for your help.

EDIT:
So I fixed the cin problem, but now there's a different problem. When my input file looks like this:

Yankee Doodle Dandee
James Cagney
Joan Leslie
Musical
Biography
1942
8
This film depicts the life of the renowned musical composer, playwright, actor, dancer and singer George M. Cohan.

X-Men
Hugh Jackman
Patrick Stewart
Action
Action
2000
7
All over the planet, unusual children are born with an added twist to their genetic code.

The list goes on, there are about 10 movies in this format. However, after running the program, the output file looks like this:

Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 

If I add an inFile.ignore(100, '\n'); to the CreateList function right below the line where I read in genre, than the output looks like this:

Title: This film depicts the life of the renowned musical composer, playwright, actor, dancer and singer George M. Cohan.
Lead Actor: 
Supporting Actor: X-Men
Genre: Hugh Jackman
Year: 0
Rating: 0
Synopsis: 
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1942
Rating: 8
Synopsis: 

EDIT: Sorry, I figured it out. It was just a matter of a few more ignores. Thanks.

codaddict
  • 445,704
  • 82
  • 492
  • 529
Michael
  • 105
  • 1
  • 4
  • 14
  • what do you mean by doesn't do anything? Do you mean that it hangs? – forsvarir Apr 12 '11 at 09:20
  • Yes, sorry about that. It just hangs. I tried putting test couts in the while loop in the input function but they didn't output. – Michael Apr 12 '11 at 09:22
  • You know you're reading from cin in the while loop as well? – forsvarir Apr 12 '11 at 09:23
  • Also, I've never really been a streams man, but does if(inFile) check if the files at the end or not, or does it just check if the files open, if it's the latter, you'll never exit the while loop – forsvarir Apr 12 '11 at 09:24
  • Could you be more specific than "doesn't do anything"? It is not just that it is awaiting input in the `cin >>` statements in `CreateList`? You migh want to have a look at [this](http://www.cplusplus.com/forum/articles/6046/). – fnokke Apr 12 '11 at 09:24
  • @forsvarir: The stream's state will be "bad" after a read past the end is attempted. Notably, _past the end_ so this loop may run for one additional, broken iteration; however, that's a new question. – Lightness Races in Orbit Apr 12 '11 at 09:28
  • @Tomalak Geret'kal: Thanks for the explanation :) – forsvarir Apr 12 '11 at 09:34
  • @fnokke: cplusplus.com is widely regarded as a poor resource, with inaccurate examples and misleading tutorials. – Lightness Races in Orbit Apr 12 '11 at 09:36

1 Answers1

6

It is not hanging, it is waiting for your input because you have:

cin >> dvdPtr -> year;   // read year from std input!!
cin >> dvdPtr -> rating;

in the function CreateList. This function opens the user specified file and reads the DVD fields from it.

Change the above lines to:

inFile >> dvdPtr -> year;   // read year from file.
inFile >> dvdPtr -> rating;
codaddict
  • 445,704
  • 82
  • 492
  • 529