Lab 5: File IO
Contents
Instructions
This lab has two parts about parsing (reading and interpreting) input files. In the first part, you'll calculate the percentage of area a county takes up in its state and in the second part, you'll scan through a file to find a contact's information.
Part 1: County area
For this part, you will create a C++ program to parse a file with two columns: a place name (e.g., a county within a state) followed by an area in square miles. The program should add a third column to the data in a new file, which is, for each line of data, the percentage of that county's area relative to the total area across all counties. So, for example, for an input file that looks like this:
Barnstable 396 Berkshire 931 Bristol 556 Dukes 104 Essex 498 Fraklin 702 Hampden 618 Hampshire 529 Middlesex 824 Nantucket 48 Norfolk 400 Plymouth 661 Suffolk 58 Worcester 1513
your program should produce an output file that looks like this:
Barnstable 396 0.05 Berkshire 931 0.12 Bristol 556 0.07 Dukes 104 0.01 Essex 498 0.06 Fraklin 702 0.09 Hampden 618 0.08 Hampshire 529 0.07 Middlesex 824 0.11 Nantucket 48 0.01 Norfolk 400 0.05 Plymouth 661 0.08 Suffolk 58 0.01 Worcester 1513 0.19
Write your program in a file called lab5-part1.cpp. Your program should read the name of the input file, the name of the output file, then carry out the computation.
HINTS: You may want to consider reading the file twice. Also, when you go to run your program, provide an output filename that is different from your input file name, or else you'll overwrite your input file.
Your program should be able to handle any input in the format described earlier. For instance, it should handle processing a file with the following New Jersey county areas:
Atlantic 569 Bergen 234 Burlington 819 Camden 221 Cape-May 267 Cumberland 500 Essex 130 Gloucester 329 Hudson 47 Hunterdon 423 Mercer 228 Middlesex 312 Monmouth 476 Morris 468 Ocean 642 Passaic 192 Salem 365 Somerset 307 Sussex 527 Union 103 Warren 362
Challenge: Formatting can be tricky. See if you can figure out how to get the spacing in the output file to look like the example above, and display the percentage to two decimal places.
Part 2: Contact
Write a C++ program that will prompt a user for a file of contacts and for the last name of a contact to search for. The program should then search the file for that contact's last name. If found, the program should print out the contact's information (phone number, email, etc.) to the console, and if not found, it should print "Contact not found." to the console. The contact file will consist of the following four lines of text per contact:
last name first name phone # email
Each line may consist of spaces (for instance, a last name could be "Van Helsing"), so be sure that your method for reading each piece of information can handle that. Here's an example input file:
De Niro Bob (555) 444-3333 bdeniro@hollywood.com Pachino Albert (555) 444-3334 al@mafia.com Hanks Tomas (777) 555-4444 th@big.com Streep Mer (333) 555-3232 streep@hotmail.com Hepburn Audie (492) 492-4924 ahep@movies.com
Try creating your own!
Challenge: Adapt the program so that it asks for the file once, but then keeps asking the user for names until the user indicates they want to stop (using some kine of sentinel value).
Submission
Submit your .cpp files from parts 1 and 2 to here. You can view the rubric here.
(Back to top)