i have a prob wit this code...it says variable letter is being used without being initialized...can anione tell me how n where should i initialize it...plz...m desperately in need of help...i m blank...m jst a beginner in c++...
the c++ code is...
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
char calc_grade(float avg);
int main() {
ifstream inp;
ofstream outp;
inp.open("C:\\Users\\Q-mar\\Desktop\\grades.txt" );
outp.open("C:\\Users\\Q-mar\\Desktop\\grades.txt" );
outp << "Student Test1 Test2 Test3 Test4 Test5 Average Grade";
string name;
float classAvg = 0;
char grade;
int k;
float avg;
for (k=1; k<=10; k++)
{
inp >> name;
outp << setw(10) << name;
calcAvg(inp, outp, avg);
outp << setw(6) << avg;
classAvg = classAvg + avg;
grade = calc_grade(avg);
outp << setw(5) << " " << grade << endl;
}
classAvg = classAvg / 10;
outp << endl << "Class Average = " << classAvg;
return 0;
}
void calcAvg(ifstream& inp, ofstream& outp, float& avg)
{
int score, k;
float sum = 0.0;
for (k=1; k<=5; k++)
{
inp >> score;
outp << setw(4) << score << " ";
sum = sum + score;
}
avg = sum / 5;
}
char calc_grade(float total)
{
char letter;
if (total >= 0)
letter = 'F';
else if (total >= 60)
letter = 'D';
else if (total >= 70)
letter = 'C';
else if (total >= 80)
letter = 'B';
else if (total >= 90)
letter = 'A';
else if (total > 100)
cout << "Grade can not exceed 100" << endl;
else
cout << "Grade can not be a negative number" << endl;
return letter;
}
the .txt file content is...
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 98
Sunny 79 85 28 93 82
Smith 85 72 49 75 63