SI204

Lab 4 Looping and File I/O

Pre-lab homework. Read parts 1 and 2 below, then turn in a flowchart for the program described in part 1 below.

Reminder: All pre-lab homework is due at the beginning of the lab period, and late pre-lab homework will earn a grade of 0.

 

  1. Write a program that reads in a list letter grades and class hours via the keyboard and reports the GPA for the given grades. Recall that grades are weighted as:

Grade

Weight

A

4.0

B

3.0

C

2.0

D

1.0

F

0.0

Grades will be entered as A, B, C, D or F, and the user will indicate that he's done by entering Q. Lower case versions of all of these must be accepted as well, but you MAY assume that invalid data is never entered.

Example run. User input appears in red

Input letter grade and hours (or Q to quit): A 3
Input letter grade and hours (or Q to quit): b 3
Input letter grade and hours (or Q to quit): a 1
Input letter grade and hours (or Q to quit): C 5
Input letter grade and hours (or Q to quit): B 3
Input letter grade and hours (or Q to quit): Q
GPA = 2.93333
  1. Modify your solution to Part 1 so that grade and credit hour information are read from files, where the user gets to enter the input file name.

Opening files when the user provides the filename:
If name is a string object with the name of the file you'd like to open, then

ifstream fin(name.c_str());
  

opens an input stream to that file, not ifstream (name), like you'd hope. The following also works:

ifstream fin;
fin.open(name.c_str());
  

The deal is this: the iostream expects a C-style string, not a C++ string object. When you have a C++ string object s, s.c_str() evaluates to the C-style version of the same string.

In these input files, there is no Q to indicate that there are no grades left. Instead, the file just ends. If the file is not found, you should print an error message and end your program. You can end your program anywhere with "exit(1)", which is available when you include the iostream library. To make your life easier, you should save these input files in the directory of your project.

grades1.txt

Program run: 1st attempt

Program run: 2nd attempt

C 3
D 2
C 5
f 3
b 3
Enter file name: Grades1.txt
Error! File Grades1.txt not found!
Enter file name: grades1.txt
Reading file grades1.txt ...
GPA = 1.6785

Some input files for you to play with are: grades1.txt, grades2.txt, and grades3.txt.

 


Going Further 

If you finish the lab assignment early you should try to tackle this problem: 

 

Look at the information in this basic html tutorial. It describes how programs can produce web pages as output. Write a program that reads in the same grade data files as in Part 2 and formats them in html as a nice table. Rules are that the table must have column headings for "grade" and "hours", and grades must be written in capital letters, regardless of whether they are capital or lower-case in the input file.

 

grades1.txt

Program run.

Output file grades1.html

Browser rendering of grades1.html

C 3
D 2
C 5
f 3
b 3
Enter input file name: grades1.txt
Enter output file name: grades1.html
<html>
<body>
<table border="2">
<tr><td>grade</td><td>hours</td></tr>
<tr><td>C</td><td>3</td></tr>
<tr><td>D</td><td>2</td></tr>
<tr><td>C</td><td>5</td></tr>
<tr><td>F</td><td>3</td></tr>
<tr><td>B</td><td>3</td></tr>
</table>
</body>
</html>

grade

hours

C

3

D

2

C

5

F

3

B

3

 


Even Further 

For a tougher challenge consider this problem: 

 

Download and save the file named "baby_names.dat".  It contains the most popular baby names of 2006 according to the U.S. Social Security Administration.  The format of of the file is as follows:

Jacob ,204760
Michael ,187948
Joshua ,173395
Emily ,170901
Matthew ,168437
Andrew ,151667

You can see for yourself by right-clicking on the file and opening it with Notepad.

 

Write a program that:

1.      Asks the user for a name.

2.      Searches the file for the name.

3.      Prints the baby name's rank and total number of babies with that name to the screen.

 


And Even Furtherer...

For the ultimate challenge consider this problem: 

 

 

The Social Security Administration does not provide a combined list of boy and girl names.  Instead it lists the top 1000 baby names of each sex in two separate files, named "boys.dat" and "girls.dat".  The file format is the same as the combined list from the previous problem.

 

Well if the SSA didn't provide a combined list, how did we get it?  We made it!  Now you will do the same.

 

Write a program that merges the two files into a single file, ordering from most popular to least.  Note that the most popular girl name is only fourth when to the two list are combined.  You will have to make sure that your program inserts the names to the new file in the proper order.

 

You may have noticed that the two individual files contain a total of 2000 names, but the combined file from the previous problem contains only 1,942 names.  What happened to those other names?  Well some names (such as Ashton, Bailey, and Jordan) are boy and girl names.  Your program will have to combine the two numbers and insert the name in the list at the appropriate position based on its combined popularity. 

 

Write a program that:

1.      Opens the two files.

2.      Combines the two lists into one list and outputs a file named, "combined_baby_names.csv"

 

Your output file must have now repeated baby names and be formatted properly:  name ,number    Don't forget the comma!!!

 

The ".csv" extension is important.  It is comma separated values file that Microsoft Excel recognizes and can read.  Confirm that your program executed properly by using Excel to open the file you produced.