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