Thanks to everyone who helped I have come upon a new problem, though. It deletes the list of movies even if I don't choose 'y' when the option comes up.
This is how my code looks now:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class movies
{
public:
void newMovie();
void listMovies();
void removeAllMovies();
};
void movies::newMovie()
{
string movieName;
int year;
ofstream tempStream ("movies.txt", ios::app);
cout << "\nInset movie name: ";
cin.get();
getline(cin, movieName);
cout << "Inset movie year: ";
cin >> year;
tempStream << movieName
<< " (" << year << ")\n";
}
void movies::listMovies()
{
string line;
ifstream tempStream ("movies.txt");
if (tempStream.is_open())
{
while (!tempStream.eof() )
{
getline (tempStream,line);
cout << line << endl;
}
tempStream.close();
}
else cout << "Unable to open file";
}
void movies::removeAllMovies()
{
char option;
cout << "\nSure you want to delete all your Movies?(y/n): ";
cin >> option;
if (option = 'y') Here is my problem
ofstream tempStream ("movies.txt", ios::trunc);
else
cout << "\nYour movies are safe !\n";
}
int main ()
{
char option;
movies myMovies;
while (option !='x')
{
cout << "Welcome to the movie Database v1.0\n\n"
<< "These are your options:\n\n"
<< " (1)List my movies\n"
<< " (2)Add new movie\n"
<< " (3)Remove all my movies\n"
<< " (x)Exit\n"
<< "\nChoose an option: ";
cin >> option;
switch (option)
{
case '1' :
myMovies.listMovies();
break;
case '2' :
myMovies.newMovie();
break;
case '3' :
myMovies.removeAllMovies();
break;
case 'x' :
break;
default :
cout << "Invalid choice !\n\n";
break;
}
}
return 0;
}