简体   繁体   中英

Testing the pointers in a linked list

I have a weird result when I run this code

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

Here is the code that I'm checking for the movietitle and if they are in the linked list i'm printing detailed information about the film. However, as an output it just prints the line that is

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

I could not understand what is the reason can anyone help?

Check to make sure your strings really are equal, and there's not a hidden \\r or \\n or trailing spaces in one of them.

Also as mentioned in the comments, you probably want your loop termination condition to be cur != NULL and not cur->next != NULL .

There are two possibilities:

  1. You have no movies in your list whose title is mtitle .
  2. You have a movie with that title, but it is the last movie in your list. Your while loop ends as soon as you get to the last movie (the one whose next == NULL ) without checking it.

Without knowing the contents of your list we cannot know which is the case here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM