Help Altering This to Fit Classes

Alright, I just got done working with structs and have been having an awkward time transferring over to classes. I have started on this, but I have no idea how to fit this to account for the whole private variable thing. What it is is a grade book program that reads a name and a set of grades from a .txt file. Here is what I am playing with at the moment. Just a nudge in the right direction will probably do wonders for me. By the way, not everything is fully implemented at the moment. I know I have the permissions askew as well:

Main:
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
#include "StudentType.h"
#include <iostream>
#include <string>
#include <fstream>
#include <climits>

using namespace std;

const int MAX_STUDENTS = 20;

void openFile(ifstream& fin);
void getData(int& actNumstu, ifstream& fin, StudentType student[]);
void computeAvg(int actNumstu, StudentType student[]);
void swapData(int actNumstu, StudentType student[]);
void print(int actNumstu, StudentType student[]);

int main () {
StudentType student[MAX_STUDENTS];
int actNumstu;
ifstream fin;
getData(actNumstu, fin, student);
computeAvg(actNumstu, student);
swapData (actNumstu, student);
print(actNumstu, student);

return 0;
}

void openFile(ifstream& fin) {
    cout << "Enter a filename: ";
  string filename;
    cin >> filename;
    cout << endl;
  fin.open(filename.c_str());
    while (!fin) {
      cout << "Please enter a VALID file name:";
      cin >> filename;
      fin.open(filename.c_str());
    }
  }

void getData (int& actNumstu, ifstream fin, StudentType student[]) {
  openFile(fin);
  actNumstu = 0;
  while (getline (fin, student[actNumstu].name) && actNumstu < MAX_STUDENTS
  && !student[actNumstu].name.empty()) {
    student[actNumstu].setName(student.reformat(name));
    for (int j = 0; j < NUM_GRADES; j++) {
      fin >> student[actNumstu].grades[j];
      student[actNumstu].setGrade(student[actNumstu].grades[j], j);
    }
    ++actNumstu;
    fin.ignore(INT_MAX, '\n' );
  }
}


My .h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H

#include <string>

const int NUM_GRADES = 10;

class StudentType {
  private:
    std::string name;
    int grades [NUM_GRADES];
    float avg;
  public:
    StudentType();
    std::string getName () const;
    void setName (std::string);
    void setGrade (int, int);
    void computeAvg ();
    std::string reformat (std::string);
    void print ();
};

#endif 


and the .cpp to boot

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
#include "StudentType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


StudentType::StudentType() {
name = "";
for (int j = 0; j < NUM_GRADES; j++) {
  grades[j] = 0;
}
avg = 0.0;
}

std::string StudentType::getName() const {
return name;
}

void StudentType::setName(std::string newname) {
name = newname;
}

void StudentType::setGrade (int grade, int num) {
grade = grades [num];
}

void StudentType::computeAvg () {
  float sum = 0;
  for (int = 0; i< NUM_GRADES; ++i){
    sum += grades [i];
  }
  avg = sum / NUM_GRADES;
}

string StudentType::reformat (std::string name) {
  int actNumstu;
  string FMName, LName;
        int pos0= student[actNumstu].getname.find_first_not_of(' ');
        int pos1= student[actNumstu].name.find_last_of(' ');
        int strsize = student[actNumstu].name.size();
        LName = student[actNumstu].name.substr(pos1, strsize- (pos1));
        FMName = student[actNumstu].name.substr(pos0, pos1);
        unsigned poscheck = FMName.find (' ');
          if (poscheck != std::string::npos){
          FMName = FMName.substr(pos0, poscheck+2);
        }
     student[actNumstu].name = LName + ", " + FMName;
}

void print (){

}
you looks to be doing fine. I am not able to understand the problem. Can you elaborate more ?
Topic archived. No new replies allowed.