Question: The lab exercise is a simple one: input movie names from a file, and output the total # of movies, the first movie, and the

The lab exercise is a simple one:

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)

For testing purposes, download an input file from course web page:

http://www.joehummel.net/cs109.html

. Under Labs, open Week

-

09, and

download the file

movies.txt

---

use the download button in the upper

-

right

to download the file to your local computer.

For c9 users, you need to upload

that file to your workspace: File menu, select

Upload Local Files..., and then

drag

-

drop the file into your workspace. It should appear in the cs109 folder

along with your main.cpp

file as shown here

------------------------------

---

---

>

The exercise is to write a complete C++ program to perform this task; note

that arrays and

functions are n

ot 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 ca

nnot u

se 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:

CS 109 : http://www.joehummel.net/cs109.html

Page

8

of

8

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):

}

Note that the last movie name will be

when

the loop hits EOF

...

For the movies.txt file

provided on the course web page

, the correct

output is

--------------------------

---------------------

>

When you run

in c9, dont forget to click in the

command window and input the filename

movies.txt. To confirm the output yourself,

open the movies.txt file in c9 or your local

computer and check the first and last movie.

Then modify the .txt file by adding a fe

w

more

movies (or delete a few), and run again,

to make

sure your program

works with a

different

dataset.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!