Hello, I am coding a program that reads from a text file of multiple rows of numbers made up of 4 columns meant to represent 4 tests taken by multiple students in one classroom.
After reading a line, the program is then supposed to calculate the average of each students and then give them a letter grade. I coded it to do so.
The problem is although the average is calculated without a problem the letter grade of the first student won't show up. Is there an error I overlooked?
Here's the code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream calcGrades;
calcGrades.open ("grades.txt");
int test1, test2, test3, test4;
int studentNum = 1;
while(calcGrades.good())
{
calcGrades >> test1 >> test2 >> test3 >> test4;
int average = (test1 + test2 + test3 + test4)/4;
char letterGrade;
if(average<60)
letterGrade='F';
if(average<=60 && average<70)
letterGrade='D';
if(average>=70 && average<80)
letterGrade='C';
if(average>=80 && average<90)
letterGrade='B';
if(average>=90)
letterGrade='A';
cout << "Student " << studentNum << "'s average is " << average << " they currently have a " << letterGrade << "." << endl;
studentNum++;
}
You've got a typo if(average<=60 && average<70) it should be >= instead of <=
However that's unnecessary http://www.cplusplus.com/doc/tutorial/control/ (if else)
using `else' would simplify your conditions