okay so hi, my problem is on (i think) case 3(the delete), it actually works when you input a movie title of example: armageddon(2012), but if you input a title that has spaces in between example: when i met your mother(2009), it just crashes. i don't know what to do anymore, although i made sure that i get the whole line, not the byCharacter or string.
the other cases are working fine.this is not a homework.:)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include<iterator>
#include<stdlib.h>
#include "MovieTime.h" //made a struct of movie,mainly a title and years.

using namespace std;
int main()
{   
    vector<string> sV;       
    int decision;
    welcomeNote:
    {
        cout<<"Welcome to MovieTime!"<<endl;
    cout<<"Choos one of the menu: "<<endl;
    cout<<"[1] Add movies to list"<<endl;
    cout<<"[2] Display movie list"<<endl;
    cout<<"[3] Delete a movie in the list"<<endl;
    cout<<"[4] Exit"<<endl;
    cin>>decision;
    }


    int numOfMovies;

    switch(decision)
    {
    case 1:
        {   system("cls");
            ofstream outfile;
            outfile.open("movieList.txt",ios_base::app);
            cout<<"how many number of movies would you like to add in the list?: ";
            cin>>numOfMovies;
            cin.get();
            MovieTime *movie=new MovieTime[numOfMovies];
            for(int i=0;i<numOfMovies;i++)
            {
                cout<<"Enter movie title: ";
                cin.getline(movie[i].title,100);
                cout<<"Enter the year the movie premiered: ";
                cin>>movie[i].years;
                cin.get();
            }

            cout<<"You have entered:(unsorted)"<<endl;
                for(int i=0;i<numOfMovies;i++)
            {
                cout<<movie[i].title;             
                cout<<"("<<movie[i].years<<")"<<endl;
                outfile<<movie[i].title<<"("<<movie[i].years<<")"<<endl;


            }

            outfile.close();
            delete[] movie;
            goto welcomeNote;
        }break;

    case 2:
        {   system("cls");
            string txt;
            ifstream file;
            file.open("movieList.txt");

              if (file.is_open())
                while (file.good())
                {
                     getline(file, txt);
                    //cout <<txt<<endl;

           //while(file>>txt)
                sV.push_back(txt);
                }
            cout << "Sorted movie list:" << endl;
              sort( sV.begin(), sV.end() );
            for(int i = 0; i < sV.size(); i++)
            { cout << sV[i] << endl;  }

             cin.get();

             cout<<endl<<endl;
             file.close();
             main();

        }break;
//  help here!:)*******************************************
    case 3:
        {   //system("cls");
            string line,deleteMovie;
            cout<<"Movie to delete: movieTitle(movieYear): ";
            cin>>deleteMovie;
            ifstream file;
            ofstream outfile;
            file.open("movieList.txt");
            outfile.open("newM.txt");
            while(getline(file,line))
            {
                if(line!=deleteMovie)
                    outfile<<line<<endl;
            }
            outfile.close();
            file.close();

            remove("movieList.txt");
            rename("newM.txt","movieList.txt");
            main();
        }break;

    case 4:{exit(EXIT_FAILURE);}break;
    default:
        { 
            goto welcomeNote;
        }

    }

    system("pause");

}

Recommended Answers

All 6 Replies

Use std::getline(cin, deleteMovie);
At present, if you entered 'when i met your mother(2009)', only 'when' would be read into deleteMovie. The behaviour you see after that is because the input stream isn't empty. Refer to http://www.daniweb.com/software-development/cpp/threads/90228/flushing-the-input-stream

Also, remove the calls to main() from your switch cases. Never use recursive calls to the programs entry point as a way of looping.

Thank you for replying, nullptr.i have been busy with the holidays. Still,it doesn't work.i am to try the gets() as soon as i get in touch with a computer (just dropped by using a smart phone). Thank you very much!

i am to try the gets()

Don't bother with that. use getline() as nullptr suggested.

ok this is the problem, i just tested its work fine :)

  while(getline(file,line))
            {
                    if(line==deleteMovie){}
                else {outfile<<line<<endl;}
            }
            outfile.close();
            file.close();

Instead of line 3 and 4, why not just this:

 if(line!=deleteMovie)
 {
     outfile<<line<<endl;
 }
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.