2012-11-14 109 views
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 " << 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(); 

}

+3

您需要向我們展示'CreateList'函數。 – codaddict

+0

輸出文件是否成功打開/創建? – SingerOfTheFall

+0

輸出文件未成功打開。當我調試時,它不會經過while循環。 – retrofusion

回答

0

您不在CreateList函數中創建列表。那麼,你是,但不是你想要的方式。看,你的清單正在創建,指針dvdPtr指向它。到現在爲止還挺好。但你這樣做:

head = dvdPtr; 

您認爲某個函數退出之後,head指針將持有的名單,但它是錯誤的。在這一行中,你正在改變一個本地指針,所以一旦函數退出,你就會失去它。這可能是你的輸出跳過while循環的原因 - 列表只是空的。您需要將引用傳遞到您的指針:

void CreateList(MovieDVD *&head, string inputFileName) 

那麼它將允許你改變它,你想要的方式。

0

想必你沒有進入while循環的。你能打印dvdPtr?這很可能是null

+0

不是這樣,我試過了。由於某種原因,它只是跳過while循環。我不明白。它不會打印出來。 – retrofusion