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,

Recommended Answers

All 2 Replies

you have to use :: operator to say that I'm going to implement
a function inside this class.

Like this.

void studentType::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();
}

and please don't use identifiers that starts with a simple letters for a class name.

OUCH!!!! Just add scope resolution operator (::) doesn't turn a function into a class member. And the member function shouldn't have to deal with any outside objects, like any particular file.

That's not a good implementation of a class method. A method is an action that a class object takes upon itself, not a whole list of "self" objects. A function that reads and stores many students would properly be a method of a "class_section" or "course" object.

How about

class studentType
{
private:
string studentFName;
string studentLName;
int testScore;
char grade;

public:

void getData ( iostream & inFile  );
//rest of methods
};

void studentType::getData( iostream & inFile )
{
           inFile >> studentLName >> studentFName >> testScore;
}

by using type iostream for the input stream, this method will work with a file input or console input.

Also consider having it return a boolean value to indicate if it actually read input, otherwise, how will you detect end of input?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.