Lab 5

Introduction:

In this lab, you will practice reading and writing files.

Writing a Few of Your Favorite Things to a File

This program will ask the user for their favorite things. The program should continue asking for their favorite things until the user is finished asking. Use a sentinel value for the user to use to indicate that they are finished entering their favorite things. In the assignment information sheet, explain your choice of sentinel value. Save the user's favorite things to a file of the user's choosing.

Here is an example of the program (user input is bold and underlined):

I'll save your favorite things to a file. What file do you want to save to? favorite.txt
Please tell me your favorite things! When you're finished, type "I'm finished telling you my favorite things."
Favorite thing: raindrops on roses
Favorite thing: whiskers on kittens
Favorite thing: bright copper kettles
Favorite thing: warm woollen mittens
Favorite thing: brown paper packages tied up with strings
Favorite thing: I'm finished telling you my favorite things.

Reading a Few of Your Favorite Things from a File

This program will open a file containing the user's favorite things and display the contents and a count of the number of favorite things in the file. Ask the user for a file containing their favorite things. If the file doesn't exist, ask again and again until the filename they give you exists. Open the file and print out each favorite thing (one favorite thing per line). Number these favorite things (as in the example below). After printing out all of the favorite things, print out the total number of favorite things.

Here is an example of the program (user input is bold and underlined):

What file contains your favorite things? favorite.txt
1. raindrops on roses
2. whiskers on kittens
3. bright copper kettles
4. warm woollen mittens
5. brown paper packages tied up with strings
These are 5 of your favorite things.

Lab Question

In the assignment information sheet, explain why you chose the file format you did. What are the advantages of it over other possible formats? What disadvantages might there be to your format?

Submission and Grading:

Complete the Assignment Information Sheet.

Submit your final program and assignment information sheet (zipped into one file) to CourseWeb in the Lab 5 assignment.

The grading rubric can be found here: Rubric (doc).

The assignment is due Monday, June 22 by 11:59 pm because of the midterm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded before the deadline will be the one graded. If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.