0
void DeleteMovie(vector<Movie> &M1){
    cout << "Option selected: delete a movie from the database" << endl;
    string T1;
    int TT=0;
    if(M1.size()<1){
    cout << ">Error: There are no movies to delete.<" << endl;
    }
    else{
        cin.ignore(100,'\n');
        cout << "Enter the title of the movie you want deleted: ";
        getline(cin,T1);
        for(int i=0;i<M1.size()+1;i++){
            if(M1[i].Get_Title()==T1){
            M1.erase(M1.begin()+i);
            cout << "Movie deleted." << endl;
            TT++;
            }
        }
    if(TT==0)
    cout << "No results found, no deletions made.";
    }
}

That's the function I'm using. I have a vector that has a movie title in it. The user enters the movie title and if there is a match, it deletes that movie from the vector.

If the user enters a movie that matches and it gets deleted, and then the user searches for a movie that has no match, the program runs fine. But if the user types in a movie that has no match first, the program crashes. Any ideas why?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by thisischris
0

if M1 has 5 titles in it your program crashes when it gets M1[5] because the 5 titles are in M1[0],M1[1],..M1[4]

for(int i=0;i<M1.size();i++){
            if(M1[i].Get_Title()==T1){
            M1.erase(M1.begin()+i);
            cout << "Movie deleted." << endl;
            TT++;
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.