hello,
I am trying to change my current code to include the following class definition.
class studentType
{
private:
string studentFName;
string studentLName;
int testScore;
char grade;
public:
void getData ( ifstream & inFile, studentType sList [ ], int size );
void calcGrade ( studentType sList [ ], int size );
int highScore ( const studentType sList [ ], int size );
void printOut ( ofstream & outFile, const studentType sList [ ], int size);
}
my current code is:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int NO_OF_STUDENTS = 20;
struct studentType{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(studentType sList[], int listSize){
ifstream inFile("students.txt");
int count = 0;
while ( count < listSize){
inFile >> sList[count].studentLName >> sList[count].studentFName >> sList[count].testScore;
count++;
}
inFile.close();
}
void calculateGrade(studentType sList[], int listSize){
int score = 0;
char grade = 'A';
for (int i = 0; i < listSize; i++){
score = sList[i].testScore;
if ( score <= 59 ){
grade = 'F';
}else if ( score >= 60 && score <= 69 ){
grade = 'D';
}else if ( score >= 70 && score <= 79 ){
grade = 'C';
}else if ( score >= 80 && score <= 89 ){
grade = 'B';
}else{ // grade 'A' for score >= 90 && score <= 100
grade = 'A';
}
sList[i].grade = grade;
}
}
int highestScore(const studentType sList[], int listSize){
int highscore = 0;
for (int i = 0; i < listSize; i++){
if ( highscore < sList[i].testScore ){
highscore = sList[i].testScore;
}
}
return highscore;
}
void printResult(const studentType sList[], int listSize){
ofstream outFile("Grade.txt");
string name = "";
outFile << left << setw(30) << "Student Name" << right << setw(10) << "Test Score" << right << setw(7) << "Grade" << endl;
for (int i = 0; i < listSize; i++){
name = sList[i].studentLName + ", " + sList[i].studentFName;
outFile << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}
outFile << endl;
int highscore = highestScore(sList, listSize);
outFile << "Highest Test Score: " << highscore << endl;
outFile << "Students having the highest test score: " << endl;
for (int i = 0; i < listSize; i++){
if ( sList[i].testScore == highscore ){
outFile << sList[i].studentLName << ", " << sList[i].studentFName << endl;
}
}
outFile.close();
}
int main(){
studentType sList[NO_OF_STUDENTS];
getData(sList, (int) NO_OF_STUDENTS);
calculateGrade(sList, (int) NO_OF_STUDENTS);
printResult(sList, (int) NO_OF_STUDENTS);
return 0;
}
any help would be appreciated,