0

このコードを実行すると、奇妙な結果になります

void MovieDatabase:: showMovie(const string mtitle){
    cout << "The informations about the" << mtitle << endl;
    for(Movie* cur = headMovie; cur ->next !=NULL; cur= cur->next){
        if(cur -> title == mtitle){
            cout <<"Movie title is "<<  cur ->title << endl;    
            //cout < "The name of director is " << cur -> firstNameOfDirector << endl;
            cout << "The last name of director is " << cur -> lastNameOfDirector <<endl;
            cout << "The year that the movie is released " << cur -> year << endl;
            cout << "The day that the movie is released " << cur -> day << endl;
            cout << "The month that the movie is released " << cur -> month << endl;
        }
    }
    cout << endl;
}

これが私が映画のタイトルをチェックしているコードであり、それらがリンクリストにある場合は、映画に関する詳細情報を印刷しています。ただし、出力として、次の行を出力するだけです。

cout << "The informations about the" << mtitle << endl;`

誰もが助けてくれる理由がわかりませんでしたか?

4

2 に答える 2

1

2つの可能性があります:

  1. リストにタイトルが。の映画はありませんmtitle
  2. あなたはそのタイトルの映画を持っていますが、それはあなたのリストの最後の映画です。ループは、チェックせずwhileに最後のムービー(のムービー)に到達するとすぐに終了します。next == NULL

あなたのリストの内容を知らなければ、私たちはここでどちらが当てはまるかを知ることができません。

于 2012-12-20T15:24:02.827 に答える
1

\r文字列が実際に等しいこと、および文字列の1つに隠しスペースや\n末尾のスペースがないことを確認してください。

また、コメントで述べたように、ループの終了条件をでcur != NULLはなく、したい場合がありcur->next != NULLます。

于 2012-12-20T15:21:01.167 に答える