3

我正在尝试编写一个允许用户跟踪其 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 " << endl出来cout << " 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();

}

4

2 回答 2

0

大概你根本没有进入while循环。可以打印dvdPtr吗?这有可能null

于 2012-11-14T06:24:22.090 回答
0

您没有在CreateList函数中创建列表。嗯,你是,但不是以你想要的方式。看,您的列表正在创建中,并且指针dvdPtr指向它。到现在为止还挺好。但是你这样做:

head = dvdPtr;

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

void CreateList(MovieDVD *&head, string inputFileName)

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

于 2012-11-14T06:38:04.610 回答