1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
Program compiles but writes "invalid letter grade" to file. Am very new to programming.
// This program calculates a final grade using a formula, assigns a
// corresponding letter grade, and outputs a "witty" comment to the user.
//******************************************************************************
#include <iostream>
#include <fstream> // For file I/O
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
// Declare function prototypes
void OpenFiles(ifstream&, ofstream&);
// Pre:
// Post: File names have been prompted for and files are opened;
// Input file name has been written on output file
void GetAndPrintStudent (ifstream&, ofstream&);
// Pre:
// Post: Name has been read from inFile and written on outFile
void InputGrades (ifstream&, float&, float&, float&, float&, float&, float&,
float&, float&, float&);
// Pre: inFile is assigned and not empty
// Post: Grades have been read from inFile
float CalculateGrades (ifstream&, ofstream&, float, float, float, float);
// Pre:
// Post:
char GetLetterGrade (ifstream&, ofstream&, char, float);
// Pre:
// Post:
string GetWittyComment(ifstream&, ofstream&, char, string);
// Pre:
// Post:
void PrintGrades (ofstream&);
// Pre:
// Post: PrintGrades prints grade earned for each grade type, overall numeric
// grade, equivalent letter grade, and witty comment have been written to the
// console and outFile
int main()
{
// Declare and open files
ifstream inFile;
ofstream outFile;
float totalHwPts = 0, totalPrgrmPts = 0, totalExamPts = 0, maxHwPts,
maxPrgrmPts, maxExamPts, hwPercent, prgrmPercent, examPercent, hwPart,
prgrmPart, examPart, finalGrd;
char LETTERGRADE;
string WittyComment;
OpenFiles (inFile, outFile);
if (!inFile || !outFile)
{
cout << "Error opening files " << endl;
outFile << "Error opening files " << endl;
return 1;
}
GetAndPrintStudent (inFile, outFile);
InputGrades (inFile, totalHwPts, totalPrgrmPts, totalExamPts, maxHwPts,
maxPrgrmPts, maxExamPts, hwPercent, prgrmPercent, examPercent);
CalculateGrades (inFile, outFile, hwPart, prgrmPart, examPart, finalGrd);
GetLetterGrade (inFile, outFile, LETTERGRADE, finalGrd);
GetWittyComment(inFile, outFile, LETTERGRADE, WittyComment);
PrintGrades (outFile);
inFile.close();
outFile.close();
system ("pause");
return 0;
}
//******************************************************************************
void OpenFiles (ifstream& inFile, ofstream& outFile)
{
string inFileName; // User-specified input file name
string outFileName; // User-specified output file name
cout << "Enter the name of the input file." << endl;
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << "Enter the name of the output file." << endl;
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << "Grade calculator for students on " << inFileName << endl;
outFile << "Grade calculator for students on " << inFileName << endl;
outFile << fixed;
}
//******************************************************************************
void GetAndPrintStudent (ifstream& inFile, ofstream& outFile)
{
string line;
getline(inFile, line); // Process student name
cout << "Student Name: " << setw(25) << line << endl;
outFile << "Student Name: " << setw(25) << line << endl;
}
//******************************************************************************
void InputGrades (ifstream& inFile, float& totalHwPts, float& totalPrgrmPts,
float& totalExamPts, float& maxHwPts, float& maxPrgrmPts,
float& maxExamPts, float& hwPercent, float& prgrmPercent,
float& examPercent)
{
int count, numHw, numPrgrm, numExam;
float hwGrade, prgrmGrade, examGrade;
inFile >> numHw;
inFile >> maxHwPts;
count = 0;
totalHwPts = 0;
while (count < numHw)
{
inFile >> hwGrade;
totalHwPts = totalHwPts + hwGrade;
count ++;
}
inFile >> hwPercent;
inFile >> numPrgrm;
inFile >> maxPrgrmPts;
count = 0;
totalPrgrmPts = 0;
while (count < numPrgrm)
{
inFile >> prgrmGrade;
totalPrgrmPts = totalPrgrmPts + prgrmGrade;
count ++;
}
inFile >> prgrmPercent;
inFile >> numExam;
inFile >> maxExamPts;
count = 0;
totalExamPts = 0;
while (count < numExam)
{
inFile >> examGrade;
totalExamPts = totalExamPts + examGrade;
count ++;
}
inFile >> examPercent;
}
//******************************************************************************
float CalculateGrades (ifstream& inFile, ofstream& outFile, float hwPart,
float prgrmPart, float examPart, float finalGrd)
{
float totalHwPts, totalPrgrmPts, totalExamPts, maxHwPts, maxPrgrmPts, maxExamPts,
hwPercent, prgrmPercent, examPercent;
if (maxHwPts > 0)
{
hwPart = (totalHwPts/maxHwPts) * (hwPercent);
return hwPart;
}
if (maxPrgrmPts > 0)
{
prgrmPart = (totalPrgrmPts/maxPrgrmPts) * (prgrmPercent);
return prgrmPart;
}
if (maxExamPts > 0)
{
examPart = (totalExamPts/maxExamPts) * (examPercent);
return examPart;
}
else
{
cout << "Error!" << endl;
outFile << "Error!" << endl;
}
finalGrd = (hwPart + prgrmPart + examPart)* 100;
return finalGrd;
}
//******************************************************************************
char GetLetterGrade (ifstream& inFile, ofstream& outFile, char LETTERGRADE,
float finalGrd)
{
if (finalGrd <= 60)
{
return LETTERGRADE;
LETTERGRADE = 'F';
}
else if (finalGrd <=69)
{
return LETTERGRADE;
LETTERGRADE = 'D';
}
else if (finalGrd <=79)
{
return LETTERGRADE;
LETTERGRADE = 'C';
}
else if (finalGrd <=89)
{
return LETTERGRADE;
LETTERGRADE = 'B';
}
else if (finalGrd <=100)
{
return LETTERGRADE;
LETTERGRADE = 'A';
}
}
//******************************************************************************
string GetWittyComment(ifstream& inFile, ofstream& outFile, char LETTERGRADE,
string WittyComment)
{
switch (LETTERGRADE)
{
case 'A': return WittyComment;
WittyComment = "A teacher's pet, I see!";
break;
case 'B': return WittyComment;
WittyComment = "You're doing an awesome job!";
break;
case 'C': return WittyComment;
WittyComment = "Keep your grades up.";
break;
case 'D': return WittyComment;
WittyComment = "Pick up the pace a bit more.";
break;
case 'F': return WittyComment;
WittyComment = "Do you want to flip burgers for the rest of your life?";
break;
default: cout << LETTERGRADE << " is not a valid letter grade."
<< endl;
outFile << LETTERGRADE << " is not a valid letter grade."
<< endl;
break;
}
}
//******************************************************************************
void PrintGrades(ofstream& outFile)
{
float hwPart, prgrmPart, examPart, finalGrd;
char LETTERGRADE;
string WittyComment;
cout << "Homework Grade is: " << fixed << setprecision(2) << setw(22)
<< hwPart << "." << endl;
outFile << "Homework Grade is: " << fixed << setprecision(2) << setw(22)
<< hwPart << "." << endl;
cout << "Program Grade is: " << fixed << setprecision(2) << setw(22)
<< prgrmPart << "." << endl;
outFile << "Program Grade is: " << fixed << setprecision(2) << setw(22)
<< prgrmPart << "." << endl;
cout << "Exam Grade is: " << fixed << setprecision(2) << setw(22)
<< examPart << "." << endl;
outFile << "Exam Grade is: " << fixed << setprecision(2) << setw(22)
<< examPart << "." << endl;
cout << "Final Grade is: " << fixed << setprecision(2) << setw(22)
<< finalGrd << "." << endl;
outFile << "Final Grade is: " << fixed << setprecision(2) << setw(22)
<< finalGrd << "." << endl;
cout << "You earned an: " << fixed << setprecision(2) << setw(22)
<< LETTERGRADE << "." << endl;
outFile << "You earned an: " << fixed << setprecision(2) << setw(22)
<< LETTERGRADE << "." << endl;
cout << WittyComment << fixed << setprecision(2) << setw(22) << endl;
outFile << WittyComment << fixed << setprecision(2) << setw(22) << endl;
}
|