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:
};
// 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.