SI204 Lab 4

  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:
    GradeWeight
    A4.0
    B3.0
    C2.0
    D1.0
    F0.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
  2. 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 stuff 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.txtProgram run: 1st attemptProgram 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.

  3. Look at the information in this basic html tutorial. It describes how programs can produce webpages 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.txtProgram run.Output file grades1.htmlBrowser 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>
    gradehours
    C3
    D2
    C5
    F3
    B3