I was asked to obtain text and numbers from a .txt file to a C++ program. What I don't know is the number of students I will be sorting their grades out so I use a while loop with the condition of !fin.eof() Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open("grades.txt");
cout << "Name\t\t\t" << "Midterm\t" << "HMAvg\t" << "Final\t" << "Average\t" << "Grade" << endl;
while(!fin.eof())
{
string name;
double mdtrm, final, hw, shw=0, ahw=0, avg;
char grade;
getline(fin, name);
fin >> mdtrm;
fin >> final;
for(int i=0; i<5; i++)
{
fin >> hw;
shw = shw+hw;
}
ahw = shw/5;
avg = ((mdtrm*0.3)+(final*0.4)+(ahw*0.3));
if(avg>=90 && avg<=100)
grade = 'A';
else if(avg>=80 && avg<90)
grade = 'B';
else if(avg>=70 && avg<80)
grade = 'C';
else
grade = 'F';
cout << name << "\t" << mdtrm << "\t" << ahw << "\t" << final << "\t" << avg << "\t" << grade << endl;
}
fin.close();
system("pause");
return 0;
}
It infinitely loops. I made sure the .txt file is ok What's wrong with my code ?