CSCI 112: Programming and Algorithms II
Fall 2005
Program 2: Movie Database
Code due: 8 am Monday September 19
(copied to your ecst turn-in directory, see below)
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 classes:

Use two classes to implement your rating system:

class Movie      // store each movie in a separate Movie object
class Movie_db // store all the Movie objects in one Movie_db

Each should be in its own files:

movie.h
movie.cpp
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 will provide sample tests for your program in the tests/p2 directory.

Each test consists on a single input file (with a name like t01) and a single corresponding output file (with a name like t01.out).  If you copy these files to your directory, you can test your program as follows:

% movies < t01 > t01.myout 2> t01.myerr
% diff t01.myout t01.out
% diff t01.myerr t01.err
%

The program diff looks for differences in files.  If there are differences it prints them.  If there are no differences, it doesn't print anything.

Thus if you do the above for each test in the p1 test directory, and diff does not print anything, you pass all of my sample tests.

However, I will use tests that I don't post to grade your program, so it is a good idea to develop some of your own tests.

If you are programming using windows, every line in your program will contain an extra hidden character (the DOS standard is different than the UNIX standard).  I will only post UNIX files.  However, you can convert your DOS files to UNIX files using the commend dos2unix.


Hints:

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 the lab machines 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 one of the department's Sun workstations (the machines in OCNL 244) before turning it in (you can log on to these machines 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.h
movie.cpp
movie_db.h
movie_db.cpp
main.cpp


copy the above files into the directory:
/user/projects/csci112/p2/USERNAME
where  USERNAME is your ecst username.
See lab2 for instructions on how to turn in the assignment if you did it on your home machine.


Late assignments:
If you do not finish by the deadline (see top for the deadline), you may turn your assignment in up to 24 hours late.  You will lose 15% for turning your assignment in 1-24 hours late.

If you turn your assignment in late, copy your assignment into the following directory
:
/user/projects/csci112/p2_late/USERNAME
Assignments will not be accepted if more than 24 hours late.