Letter Grade Output Text File

I am currently trying to add loop that will give me a letter grade. This is the program i currently have. However i cannot get the ;etter grade to correspond with the equivalent average


#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

ifstream inn; // This is an object targets student name
ifstream ing; // This target student grade
ofstream out; // This targets output file



const int row = 5; // Needed to modify entire program instead of going line by line
const int col = 6;

// ====================Definition For Functions =======================================================
void ReadData(string stname[row], int stgrade[row][col])
{

inn.open("Students.txt");
ing.open("Grades.txt");
int r, c;


for (r = 0; r < row; r++)
{
getline(inn, stname[r]);
for (c = 0; c < col; c++)
ing >> stgrade[r][c];
}
}
// =======================Definition for display======================================
void DisplayData(string stname[row], int stgrade[row][col], int sum[row], double avg[row], char letg [row])
{
out.open("Result.txt");
int r, c;
cout << fixed << showpoint << setprecision(2);
for (r = 0; r < row; r++)
{
cout << setw(20) << left << stname[r];
for (c = 0; c < col; c++)
cout << setw(5) << right << stgrade[r][c];
cout << setw(6) << right << sum[r] << setw(7) << right << avg[r] << setw(8) << right << letg [r];
cout << endl;
}
}

//=================================== Find sum of Grade ==============================
void total(int stgrade[row][col], int sum[row])
{
int r, c;
for (r = 0; r < row; r++)
{
for (c = 0; c < col; c++)
sum[r] = sum[r] + stgrade[r][c];
}
}
//================================ Find Average of Grade=================================
void average(int sum[row], double avg[row])
{
int r;
for (r = 0; r < row; r++)
avg[r] = sum[r] / static_cast<double> (col);

}

//================================ Find the letter grade of average======================
void lettergrade(double avg[row], char letg[row])
{
int r;
for (r = 0; r < row; r++)

if (avg[r] == 100 || avg[r] > 90)
letg[r] = 'A';
else if (avg[r] < 90 || avg [r] <80)
letg[r] = 'B';

}

int main()
{
string stn[row];
int stg[row][col];
int stt[row] = { 0 };
double sta[row] = { 0.0 };
char stl[row] = { 0 };

ReadData(stn, stg);
total(stg,stt);
average(stt,sta);
lettergrade(sta, stl);
DisplayData(stn, stg, stt, sta,stl);


inn.close();
ing.close();
out.close();

system("pause");
return 0;


}

if (avg[r] == 100 || avg[r] > 90)
letg[r] = 'A';
else if (avg[r] < 90 || avg [r] <80)
letg[r] = 'B';

read what you wrote.
if avg is 100 OR it is > 90 its an A.
if its <90 or < 80 its a B.

first, if its > 90, its an A, apparently. why do you care if its == 100?
second, the double less than is useless. Everything < 80 is already < 90. Redundancy aside, its also probably wrong. You probably want >80.
finally, equality? What if avg is exactly 90, what result do you expect? Is 90 > 90? no. Is 90 < 90? no. Should exactly 90 be an A? If so you want >= 90. If not, >80 for Bs will pick it up.

You are probably doing way too much work. Perhaps a lookup table?
string avgs = "FFFFFFDCBAA"
answer = avgs[avg/10]; //say avg = 83. 83/10 is 8. avgs[8] is the letter B.
A is in there twice so 9 and 10 result both work.

Last edited on
Topic archived. No new replies allowed.