changing a structure to a class

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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
Obviously, the first step is to copy and paste the class decl over top of the struct decl. Then you'll get some compile errors since the private members can't be directly accessed. It looks like the global functions will now be static class member functions of studentType. The main function would call the static functions for operating on the array of student classes. You can have arrays of class instances just as easily as arrays of struct instances. The tricky part will be in designing the non-static member functions for filling each student object.

The first thing you should do with the student class is define a default constructor that initializes the attributes to NULL values (0 for the char and int but "" for the string). Then you need methods for filling the data with values and accessing them so that they can be printed. Take a shot at this on your own and let us know when you have more specific questions.
you have given the solution yourself.. what help you want.

you have the code and you have the class declaration also.. what else is left.
Topic archived. No new replies allowed.