输出链接列表?

时间:2012-11-14 06:21:45

标签: c++ linked-list

我正在尝试编写一个程序,允许用户跟踪他们的DVD。使用包含这些属性的输入文件。

基本上,用户应该能够输入输入文件的名称。 使用结构和指针,在输入文件中创建了电影的链接列表。 接下来,将链接列表输出到输出文件。 格式化绘图的输出,使其自动换行。

输出假设看起来像这样

***************************************************************************
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.
***************************************************************************

到目前为止,问题是我的输出,我无法显示输出。我尝试调试它,结果发现我陷入了while循环。

这是我的输出功能

 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);

我在输出列表中仅为我的调试添加了cout cout << "Before While Output " << endlcout << " DEBUG -- AFTER WHILE OUTPUT" << endl出来并跳过while循环。请帮帮我。

我的广告素材列表功能

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();

}

2 个答案:

答案 0 :(得分:0)

据推测,你根本没有进入while循环。你能打印dvdPtr吗?可能是null

答案 1 :(得分:0)

您没有在CreateList功能中创建列表。嗯,你是,但不是你想要的方式。请参阅,您的列表正在创建,指针dvdPtr指向它。到现在为止还挺好。但是你这样做了:

head = dvdPtr;

您假设在函数退出后,head指针将保留列表,但这是错误的。在这一行中,您正在更改本地指针,因此一旦该函数退出,您将丢失它。这可能是您的输出跳过while循环的原因 - 列表只是空的。您需要传递对指针的引用:

void CreateList(MovieDVD *&head, string inputFileName)

然后它将允许您以您想要的方式更改它。