1

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++;
    }

    return 0;
}

And here's the content of the file it reads from:

44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95

Thank you for any help given, I've been agonizing over this and I just can't see what went wrong.

DevSolar
  • 59,831
  • 18
  • 119
  • 197
  • 1
    See also http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user657267 Apr 15 '16 at 08:02
  • no idea who voted to close this for lack of code-not-working info. The observed output is clearly mentioned, the code is MCVE, and the input data is provided. Rather, it should be marked due-to-typo and non-reproducible once said-typo is addressed. – WhozCraig Apr 15 '16 at 08:05
  • "I coded it to do so." -- Computers do **exactly** what we tell them to do. If the observed output is not as expected, we didn't tell them what we think we told them, and have to *check our assumptions*. ;-) – DevSolar Apr 15 '16 at 08:10

1 Answers1

0

Correct logic should be:

if(average<60)
letterGrade='F';
else if(average>=60 && average<70) 
//             ^^
letterGrade='D';
else if(average>=70 && average<80)
letterGrade='C';
else if(average>=80 && average<90)
letterGrade='B';
else if(average>=90)
letterGrade='A';
vcp
  • 972
  • 8
  • 15
  • @ VCP, Any idea how reputation get improved and how this whole Stackoverflow works, I am new to it. – Atul Agarawal Apr 15 '16 at 08:13
  • @AtulAgarawal: [What is reputation](http://stackoverflow.com/help/whats-reputation). Actually, check the whole FAQ, it's quite well-written. – DevSolar Apr 15 '16 at 08:14