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?

Recommended Answers

All 2 Replies

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

wow, can't believe I missed that. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.