calculate Final Letter Grades a class

hello there. i need to do a program that calculate the final grade of a list of students that is in a datafile.txt i have 4 datafiles in for of a list 1 student's names the other one is the midexam grades, the other the finalexam grades, and the quizes grades. the problem i have is that when i open the file it only show me the first name in the list and the calculation of the first student many times i do not know how to do to show all the list with their final grades. ill apriciateany help thank you.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void myMenu()
{
cout <<""<< endl;
cout <<" Please enter your selection"<<endl;
cout <<""<< endl;
cout << " Enter # 1 to enter new student data"<< endl;
cout << " Enter # 2 to show "<< endl;
cout << " Enter # 3 to exit"<< endl;

}

class myStudent
{
private:
char lName[26];
int midExam;
int finExam;
double aveAssign;
double finGrade;

public:
void getData();
void showFinGrade();
double calFinGrade();

};

void myStudent::getData()
{

string line;
ifstream myfile ("FristName.txt");// read file
ifstream myfile1 ("MidtermExam.txt");
ifstream myfile2 ("FinalExam.txt");
ifstream myfile3 ("QuizAve.txt");

if (myfile.is_open()) // do it only if the file is open
{
//while (!myfile.eof()) // to the end of file
//{
//for (int i=0; i<46; ++i )
{
myfile >> lName;
myfile1 >> midExam;
myfile2 >> finExam;
myfile3 >> aveAssign;//[k]; // Here we are writing to the array
//cout << lName <<'\t'<< endl;
}


myfile.close();
}
else cout << " Ooops we cannot open file" << endl;


//cout <<" Please enter student's last name: ";
//cin >> lName;
//cout <<" Please enter student's midterm exam grade: ";
//cin >> midExam;
//cout <<" Please enter student's final exam grade: ";
//cin >> finExam;
//cout <<" Please enter student's average Assignment grade: ";
//cin >> aveAssign;
}


void myStudent::showFinGrade()
{
cout <<setw(12)<<lName<<setw(15) <<finGrade<<endl;
}

double myStudent::calFinGrade()
{

finGrade = 0.35*midExam + 0.3*aveAssign + 0.35*finExam;

return finGrade;
}


int main()
{
myStudent student [5]; // Max student number is 5

int option;
double sum =0;

myMenu();
cin >> option;

while (option!=3)
{
if (option == 1)
{
for (int i =0; i <5; i++)
{
//cout <<""<< endl;
//cout<<" This is student "<<endl;//i+1<<"."<<endl;
student[i].getData();
}


}


else if (option == 2)
{
cout <<setw(10)<<"Last Name"<<setw(15)<<"Final Grade"<< endl;
for (int i = 0; i<5; i++)
{
student[i].calFinGrade();
student[i].showFinGrade();

}

}


else
{
cout <<""<< endl;
cout<<"================="<< endl;
cout<<" INVALID ENTRY "<< endl;
cout<<"================="<< endl;


}


myMenu();
cin >> option;

}

cout <<""<< endl;
cout<<"=============================================="<< endl;
cout<<" EXITING FROM THE PROGRAM "<< endl;
cout<<"============================================="<< endl;
system ("pause");
return 0;
}
Last edited on
Yep there,s your problem.


system ("pause");

the program thinks it's an integer.
Nop :( i took out the system pause and its the same
In myStudent::getData() you close the file FristName.txt.

So everytime you call myStudent::getData() and open the file again you start reading from the start of that file.

Maybe all files are closed when they go out of scope in myStudent::getData() and so you only read first entries in all of them.
Last edited on
nothing if i write the calculation and the part that show the final grade into the part that open file it works but i dont want that
You could open the files in main() and pass them as references to getData().
Topic archived. No new replies allowed.