Grades will be entered as
Grade Weight A 4.0 B 3.0 C 2.0 D 1.0 F 0.0
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
Opening files when the user provides the filename:In these input files, there is no
Ifname
is astring
object with the name of the file you'd like to open, thenifstream fin(name.c_str());opens an input stream to that file, notifstream (name)
, like you'd hope. The following also works:ifstream fin; fin.open(name.c_str());The deal is this: theiostream
stuff expects a C-style string, not a C++string
object. when you have a C++string
objects
,s.c_str()
evaluates to the C-style version of the same string.
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 3Enter 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
.
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