1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include "Movie.h"
using namespace std;
//removes duplicate movie titles
template <typename T>
void remove_duplicates(vector<T>& vec)
{
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
//-------------------------------------
//Main program ------------------------
//-------------------------------------
int main () {
vector<Movie> movies;
ifstream fin("CS172_Spring2014_Movie_inputs.txt");
ofstream fout("movie.txt");
//read in file until end of file
while ( !fin.eof() ) {
string data;
Movie movie;
getline(fin, data, '\n'); //reads data from file
movie.setTitle(data); //sets next data to movie title
getline(fin, data, '\n');
movie.setDirector(data); //sets next data to Director
getline(fin, data,'\n');
int year = atoi(data.c_str()); //converts data string to int
if(year==0) {
Movie_Rating rating = movie.rate_from_string(data); //converts and assigns string data to Movie_Rating type object
movie.setRating(rating); //sets next data to movie rating
}
else {
movie.setYear(year); //sets next data to year
}
getline(fin, data, '\n');
year = atoi(data.c_str());
if(year==0) {
Movie_Rating rating = movie.rate_from_string(data); //converts and assigns string data to Movie_Rating type object
movie.setRating(rating);
}else {
movie.setYear(year);
}
//sets next data to movie rating
getline(fin, data, '\n');
movie.setURL(data); //sets next data to imdb URL
getline(fin,data,'\n');
//read actors in file until $$$$
while( data != "$$$$"){
movie.addActor(data); // sets next data to actors until $$$$ is encountered
getline(fin,data,'\n');
}
movies.push_back(movie);
}
remove_duplicates(movies); //removes duplicate movie titles
for(int i=0; i< movies.size(); i++) {
movies[i].output(cout); //print to screen
movies[i].output(fout); //print to movie.txt file
}
return 0; //program done
}
|