CSCI 112: Programming and Algorithms II
Program 2: Movie Database

Grading Weight: 2 (programs will have a weight between 1 - 5)


Overview:
Read a collection of movie ratings (each containing a title, year, and rating) and calculate an average rating for each movie.

For example, given the following input (this is the required input format, see below):

Harold & Kumar Go to White Castle
2004
9
Fahrenheit 9/11
2004
8
There's Something About Mary
1998
9
Star Wars
1977
7
Harold & Kumar Go to White Castle
2004
4
Star Wars
1977
6

The average ratings would be (this is the required output format, see below);

2004: Harold & Kumar Go to White Castle, rating = 6.5
2004: Fahrenheit 9/11, rating = 8
1998: There's Something About Mary, rating = 9
1977: Star Wars, rating = 6.5


Notice that if a movie appears only once in the input (such as "Fahrenheight 9/11" above), its printed rating is that given in the single rating.  If a movie appears multiple times, its printed rating is the average of all the input ratings.


Program Requirements:

Read movie ratings from standard input.  The input will be in this exact form:

movie title
year
rating
movie title
year
rating
...

There may be 0 or more ratings.  The movie title, year, and rating will always be on their own line.  The movie title will be an arbitrary string and may contain any printable characters (including spaces).  The year and the rating will be integers.  We will only test your program with input that meets these requirements, and thus your program does not have to handle bad input.

A movie may appear one or more times in the input.

After all the input has been read, write all ratings to standard output using the following form.  Make sure you write them in the same order as in the input:
year: movie title, rating = n

Note: n is a double.  Notice that there is exactly one space after the :, no spaces after the title, one space after the "," and one space before and after the "=".  There are no spaces after the n.

If there are no ratings in the input (i.e. the input is empty), write nothing to standard output.

Your system should be able to handle up to 100 different movies and any number of ratings.  If more than 100 movies are given, your program should print the following to standard error (cerr << "Too many ...") and print nothing to standard output.

Too many movies specified.
The system can only handle 100 movies.
Giving up...

Required class:

You must create a class to store the movie database:

class Movie_db // store all the Movie objects in one Movie_db

This class should be in its own files:

movie_db.h
movie_db.cpp

In addition to these files, you will need to turn in a file containing your main() function:

main.cpp



Testing Your Program:

I have posted some sample tests in the tests/p2 directory.

Since this program is required to write error messages to standard error, the test files include the correct error output for each test (e.g. t001.myerr).  See the testing p2 instructions for instructions on how to check your error output.

I will test your program with additional tests not posted in the test directory.  It is a very good idea to design and implement your own set of tests.


Hints:

Use a structure to hold the information about each movie.  Since this structure will only be used by the Movie_db class, you can nest it inside of the Movie_db class:

class Movie_db
{
public:
...
private:
struct Movie
{
...
};
//  now you can use struct Movie
};

If you would rather you can use a class for Movie instead of a struct, however make all of its members public and do not provide a constructor.  A struct is very similar to a class with all the members public.  You cannot provide a constructor because we don't have all the movie data when we construct the array of Movies.  

When too many different movies are entered, the program is suppose to print an error message and then do nothing else.  The easiest way to achieve this is to call exit(1) after you print the error message.  The exit() function immediately stops the programs.  The 1 means that there was an error.  The 1 is returned to the calling process (usually the shell).  If you ever want to terminate a program that does not have an error, call exit(0).

Use the getline() function to read the title string.  getline() will return 0 when called after all the input has been read.  In other words, getline() returns 0 when it reads end of file (EOF).
while (getline(cin, title) != 0)   // while the getline did not return end of file
{
// now read the year and the rating
...
}

Since the year and rating are integers, it is easiest to read them using the standard cin operator:
cin >> year;
cin >> rating;

A problem occurs when you combine getline() with cin >>.  The problem is that the cin >> does not read the end of line marker ('\n') and thus the next time you call getline(), you read an empty line.  Thus you have to manually read that end of line to get it out of your way:
cin >> year;
cin >> rating;
cin.ignore();

Windows warning:  If you are programming in the microsoft windows world, this won't work.  Windows has two characters at the end of each line.  You can fix the problem by using the command dos2unix to turn your input files into UNIX like files (that is, with only a '\n' at the end of each line).  It would not be a good idea to write your code so that it only works in the windows world because we will grade it in the UNIX world.

An alternative to using the above approach for reading the integer rating is to read the integer as a string and then convert the string to an integer.  This approach is not as straightforward as the approach above, so you should only try it if you have extra time.  Hint: read about the function atoi().

Sample input and output can be found in tests/p2.

General Requirements:
I will deduct 10 percent if your program does not compile on a Linux machine using my Makefile.  Thus you must make sure to use the filenames listed below (do not capitalize your filenames), and if you are working on windows, you should compile and test your program on a Linux machine before you turn it in r (you can log on to jaguar.ecst.csuchico.edu from home using the putty program).

We will grade your program using another program, so if your program does not work exactly as specified above you will lose points.  For example, if you put an extra space at the end of the line, or a blank line at the end of the output, you will lose points.  Test your program using the sample tests in the test directory (see above).

The first lines of all your files (.h and .cpp) must contain:


// last name, first name
// ecst_username@ecst.csuchico.edu
// 112 Program p2: Movie Database




How to turn in code:
You must turn in the following files:

movie_db.cpp
movie_db.h
main.cpp

See the turn in instructions for details on how to turn in this assignment.

The turn in instructions contain details on turning in a late assignment.