Question: Write a C++ program that, input movie names from a file, and output the total # of movies, the first movie, and the last movie.
Write a C++ program that,
input movie names from a file, and output the total # of movies, the first movie, and the last movie. The input file contains the movie names, one per line
---
there is nothing special at the end. Example: suppose the file movies.txt contains
The Shawshank Redemption (1994)
The Godfather (1972)
The Godfather: Part II (1974)
Pulp Fiction (1994)
The Good the Bad and the Ugly (1966)
The Dark Knight (2008)
12 Angry Men (1957)
Schindler's List (1993)
The Lord of the Rings: The Return of the King (2003)
Fight Club (1999)
Your program should input the name of the file from the keyboard:
movies.txt
The output from your program should be the # of movies, first movie name, and last movie name:
10
The Shawshank Redemption (1994)
Fight Club (1999)
*Note : ( THE PROGRAM OUTPUT MUST BE IN THE EXACT SAME ORDER)
The exercise is to write a complete C++ program to perform this task; note that arrays and functions are not required but its good practice to use them if you want. Assume the input file contains at least 1 movie, and at most 1000 movies. There are two aspects of this exercise that are new... First, since the movies names contain spaces, we cannot use the >> operator to input a movie name. Instead, we must use the C++getline() function which reads an entire line:
string moviename;
file >> moviename; // does not work
getline
(file, moviename); // read entire line as the movie name:
Second, because there is no special marker at the end of the file, we have to change the input pattern slightly. Instead of looping until we see -1 or #, we loop until we see EOF --- end of file. The input pattern is now:
getline (file, moviename); // read first movie:
while (!file.eof() ) //while not EOF marker:
{
.
.
.
getline(file, moviename); // read next movie (will be empty string @ EOF):
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
