I'm working on a movie class for a project. I think I've got it reading in movies from a file. I want to test it using the overloaded subscript operator, but the Driver file will not let me enter a movie title to search for. I can't think of a reason why it will not let me enter a movie name.
/* Movie.h
Header file for the Movie class.
Author: Christopher Beech
Created: October 7, 2008
Modified: October 22, 2008
*/
#ifndef MOVIE_H
#define MOVIE_H
#include <iostream>
#include <string>
#include <cstring>
enum MovieGenre {action, comedy, drama, horror, science_fiction, ANY}; //Declares an enum with the possible movie genres.
using namespace std;
class Movie
{
public:
/**
Default constructor.
*/
Movie();
/**
Constructs a movie with the given arguments.
*/
Movie(string movie_title, string movie_director, string movie_cast, MovieGenre the_genre, int number_of_hits);
/**
Returns the title of the movie.
*/
string get_title() const;
/**
Returns the number of search hits of a movie.
*/
int get_hits() const;
/*
Searches the movie record for words appear in the keyword list. Returns true if they are found, false otherwise.
*/
bool search(string keywordList, MovieGenre whichGenre = ANY);
/**
Compares the movie of hits from the left movie with the number of hits from the right movie. Returns true if the number
from the left movie is greater than the number from the right movie. In the case of a tie, returns true if the title of
the left movie is lexicographically smaller than the right movie.
*/
friend bool operator>(const Movie& left, const Movie& right_one);
/**
Prints the data members of a movie record.
*/
friend ostream& operator<<(ostream& out, const Movie& m);
private:
string title; //Creates a string variable named title.
string director; //Creates a string variable named director.
string cast; //Creates a string variable named cast.
MovieGenre value; //Creates a MovieGenre variable named value.
int hits; //Creates a int variable named hits.
};
#endif
/* MovieDatabase.h
Header file for the MovieDatabase class.
Author: Christopher Beech
Created: October 7, 2008
Modified: October 22, 2008
*/
#ifndef MOVIEDATABASE_H
#define MOVIEDATABASE_H
#include "Movie.h"
#include <string>
#include <iostream>
using namespace std;
class MovieDatabase
{
public:
/**
Default constructor.
*/
MovieDatabase();
/**
Copy constructor.
*/
MovieDatabase(const MovieDatabase& rhs);
/**
Function to overload the assignment operator.
*/
MovieDatabase& operator=(const MovieDatabase& p);
/**
Function to overload the []
*/
int operator[](string title) const;
/**
Function to add a new movie.
*/
void addMovie(string title, string director, string cast, MovieGenre genre);
/**
Function to display records that cause the search function of the Movie class to return true.
*/
void searchMovies(string keywordList, MovieGenre genre = ANY);
/**
Function to display records whose number of hits is at least minHits.
*/
void listHotMovies(int minHits);
/**
Overloaded friend extraction operator that adds movies to the array.
*/
friend istream& operator>>(istream& in, MovieDatabase& r);
/**
Destructor for the class.
*/
~MovieDatabase();
private:
Movie *Movies; //Declares a Movie pointer variable named Movie.
int counter; //Declares an int variable named counter.
int capacity; //Declares an int variable named physical_size.
static int const initial_capacity = 50; //Declares a static int variable named capacity and sets it equal to 50.
static int const expansion = 20; //Declares a static int variable named expansion and sets it equal to 20.
};
#endif
/* Movie.cpp
Cpp file for the Movie class.
Author: Christopher Beech
Created: October 7, 2008
Modified: October 22, 2008
*/
#include "Movie.h"
#include "MovieDatabase.h"
#include <string>
#include <iostream>
#include <cctype>
#include <sstream>
using namespace std;
Movie::Movie()
{
title = '\0';
}
Movie::Movie(string movie_title, string movie_director, string movie_cast, MovieGenre the_genre, int number_of_hits)
{
title = movie_title;
director = movie_director;
cast = movie_cast;
value = the_genre;
hits = number_of_hits;
}
string Movie::get_title() const
{
return title;
}
int Movie::get_hits() const
{
return hits;
}
/*bool search(string keywordList, MovieGenre whichGenre = ANY)
{
const size_t length = keywordList.size();
string a_name;
int physical_size;
while(getline(cin, keywordList))
{
for(int i = 0; i != length; i=i+1)
{
keywordList[i]=tolower(keywordList[i]);
}
if(whichGenre == 'a' || 'c' || 'd' || 's' || 'h' || 'any')
{
istringstream instream(keywordList);
instream >> a_name;
for(int i=0; i<physical_size; i++)
{
if(&(Movie->[i].getTitle()) = a_name)
{
return true;
}
else
{
return false;
}
}
}
else
{
istringstream instream;
instream.str(keywordList);.
instream >> a_name;
for(int i=0; i<physical_size; i++)
{
if(Movie[i] = a_name)
{
return true;
}
else
{
return false;
}
}
}
}
}*/
bool operator>(const Movie& left, const Movie& right)
{
int a = left.hits;
int b = right.hits;
string c = left.title;
string d = right.title;
if (a > b)
{
return true;
}
else if (a == b)
{
if (c < d)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
ostream& operator<<(ostream& out, const Movie& a)
{
int genre;
string type;
if (a.get_title().empty() == true)
{
out << endl;
return out;
}
else
{
out << "Title: " << a.title << endl;
genre = a.value;
switch(genre)
{
case '0':
type = action;
break;
case '1':
type = comedy;
break;
case '2':
type = horror;
break;
case '3':
type = science_fiction;
break;
case '4':
type = drama;
break;
default:
type = ANY;
}
out << "Genre: " << type << endl;
out << "Director: " << a.director << endl;
out << "Cast: " << a.cast << endl;
out << "Hits: " << a.hits << endl;
return out;
}
}
/* MovieDatabase.cpp
Cpp file for the MovieDatabase class.
Author: Christopher Beech
Created: October 7, 2008
Modified: October 22, 2008
*/
#include "Movie.h"
#include "MovieDatabase.h"
#include <string>
#include <iostream>
using namespace std;
MovieDatabase::MovieDatabase()
{
Movies = new Movie[MovieDatabase::initial_capacity];
counter = 0;
}
MovieDatabase::MovieDatabase(const MovieDatabase& rhs)
{
capacity = counter;
Movies = new Movie[capacity];
counter = 0;
for (int i=0; i<capacity; i++)
{
Movies[i]=rhs.Movies[i];
}
}
MovieDatabase& MovieDatabase::operator=(const MovieDatabase& p)
{
if(this != &p)
{
delete [] Movies;
capacity = counter;
Movies = new Movie[capacity];
counter = 0;
for (int i=0; i<capacity; i++)
{
Movies[i]=p.Movies[i];
}
}
return *this;
}
int MovieDatabase::operator[](string title) const
{
Movie the_title;
for(int i=0; i<capacity; i++)
{
if(title == Movies[i].get_title())
{
cout << *(Movies);
}
else
{
the_title = Movie();
cout << the_title;
}
}
return 0;
}
/*
void addMovie(string title, string director, string cast, MovieGenre genre)
{
for(int i=0; i<capacity; i++)
{
if(Movies[i]->get_title() != title)
{
if(capacity == 50)
{
Movie = new Movie[capacity + MovieDatabase::expansion]
Movie[i]=p.Movies[i];
}
else
{
int hits = 0;
Movie(title, director, cast, genre, hits);
}
}
else
{
string oldDirector = director;
string oldCast = cast;
MovieGenre oldGenre = genre;
}
}
}
void searchMovies(string keywordList, MovieGenre genre = ANY)
{
if(a.search(keywords, enum_value) == true)
{
}
else
{
}
if(a.search(keywords)== true)
{
}
else
{
}
}
void MovieDatabase::listHotMovies(int minHits)
{
for(int run=0; run<capacity-1; run++)
{
int smallest = run;
for(int i=run+1; i<capacity; i++)
{
if (Movie[i] < Movie[run])
{
run = i;
}
}
int temp = Movie[pass];
Movie[pass] = Movie[smallest];
Movie[run] = temp;
}
}
*/
istream& operator>>(istream& in, MovieDatabase& r)
{
string title;
string blank;
char genre;
MovieGenre enum_genre;
string director;
string cast;
int hits;
for (int i = 0; i < MovieDatabase::initial_capacity; i++)
{
getline(in, title);
in >> genre;
switch(genre)
{
case 'a':
enum_genre = action;
break;
case 'c':
enum_genre = comedy;
break;
case 'h':
enum_genre = horror;
break;
case 's':
enum_genre = science_fiction;
break;
case 'd':
enum_genre = drama;
break;
default:
enum_genre = ANY;
}
getline(in, blank);
getline(in, director);
getline(in, cast);
getline(in, blank);
hits = 0;
Movie a (title, director, cast, enum_genre, hits);
a = r.Movies[i];
r.counter = r.counter + 1;
}
return in;
}
MovieDatabase::~MovieDatabase()
{
delete [] Movies;
}
/* Driver.cpp
Driver file for the Movie and MovieDatabase class.
Author: Christopher Beech
Created: October 7, 2008
Modified: October 22, 2008
*/
#include "Movie.h"
#include "MovieDatabase.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int minHits;
int next;
char a_genre;
MovieGenre enum_genre;
string blank;
string keywords;
string request;
string the_cast;
string the_director;
string the_genre;
string the_name;
string wish;
fstream fs;
MovieDatabase a;
if (argc == 1)
{
cerr << "Please enter the file path on the command line." << endl;
return 1;
}
fs.open(argv[1], ios::in | ios::out);
while (fs >> next)
{
getline(fs, the_name);
getline(fs, the_genre);
getline(fs, the_director);
getline(fs, the_cast);
getline(fs, blank);
fs >> a;
}
//Creates menu.
cout << "*************************************" << endl;
cout << "l/L : Look up a record in database." << endl;
cout << "a/A : Add or update a record in database." << endl;
cout << "s/S : Search database using keywords." << endl;
cout << "h/H : Display the records that has at least minimum hits." << endl;
cout << "q/Q : Quit." << endl;
cout << "*************************************" << endl;
while (true)
{
cout << "Enter the operation you want to perform" << endl;
cin >> request;
if (request == "l" || request == "L")
{
cout << "Please enter the title of the movie: ";
getline(cin, the_name);
cout << a[the_name];
}
else if (request == "a" || request == "A")
{
cout << "Please enter the title of the movie: " << endl;
getline(cin, the_name);
cout << "Please enter the director: " << endl;
getline(cin, the_director);
cout << "Please enter the cast: " << endl;
getline(cin, the_cast);
cout << "Please enter the genre:";
cout << "a for action, c for comedy. h for horror";
cout << "s for science fiction, d for drama, or nothing for any." << endl;
cin >> a_genre;
//Switch statement to determine the genre.
switch(a_genre)
{
case 'a':
enum_genre = action;
break;
case 'c':
enum_genre = comedy;
break;
case 'h':
enum_genre = horror;
break;
case 's':
enum_genre = science_fiction;
break;
case 'd':
enum_genre = drama;
break;
default:
enum_genre = ANY;
}
//a.addMovie(the_name, the_director, the_cast, enum_genre);
}
else if (request == "s" || request == "S")
{
cout << "Please enter keywords: " << endl;
getline(cin, keywords);
cout << "Do you wish to enter a genre?" << endl;
cin >> wish;
if (wish == "y" || wish == "Y")
{
cout << "Please enter the genre:";
cout << "a for action, c for comedy. h for horror";
cout << "s for science fiction, d for drama, or nothing for any." << endl;
cin >> a_genre;
//Switch statement to determine the genre.
switch(a_genre)
{
case 'a':
enum_genre = action;
break;
case 'c':
enum_genre = comedy;
break;
case 'h':
enum_genre = horror;
break;
case 's':
enum_genre = science_fiction;
break;
case 'd':
enum_genre = drama;
break;
default:
enum_genre = ANY;
}
//a.searchMovies(keywords, enum_genre);
}
else if (wish == "n" || wish == "N")
{
//a.searchMovies(keywords);
}
else
{
cout << "Invalid command!" << endl;
return 1;
}
}
else if (request == "h" || request == "H")
{
cout << "Please enter the number of hits desired: " << endl;
cin >> minHits;
//a.listHotMovies(minHits);
}
else if (request == "q" || request == "Q")
{
cout << "Quitting program." << endl;
fs.close();
return 0;
}
else
{
cout << "Invalid command!" << endl;
return 1;
}
}
}