I cannot get my program to compile. please help!!

I keep receiving these errors when I run the compiler:

Program04.cpp:21:14: error: variable or field âgetDataâ declared void
Program04.cpp:21:14: error: âStudentTypeâ was not declared in this scope
Program04.cpp:21:26: error: expected primary-expression before â]â token
Program04.cpp:21:29: error: expected primary-expression before âintâ
Program04.cpp:23:15: error: variable or field âsortDataâ declared void
Program04.cpp:23:15: error: âStudentTypeâ was not declared in this scope
Program04.cpp:23:27: error: expected primary-expression before â]â token
Program04.cpp:23:30: error: expected primary-expression before âintâ
Program04.cpp:27:22: error: variable or field âcomputeAveragesâ declared void
Program04.cpp:27:22: error: âStudentTypeâ was not declared in this scope
Program04.cpp:27:34: error: expected primary-expression before â]â token
Program04.cpp:27:37: error: expected primary-expression before âintâ
Program04.cpp:29:16: error: variable or field âprintDataâ declared void
Program04.cpp:29:16: error: âStudentTypeâ was not declared in this scope
Program04.cpp:29:28: error: expected primary-expression before â]â token
Program04.cpp:29:31: error: expected primary-expression before âintâ
Program04.cpp: In function âint main()â:
Program04.cpp:34:1: error: âStudentTypeâ was not declared in this scope
Program04.cpp:34:13: error: expected â;â before âstudentsâ
Program04.cpp:37:9: error: âstudentsâ was not declared in this scope
Program04.cpp:37:19: error: ânâ was not declared in this scope
Program04.cpp:37:20: error: âgetDataâ was not declared in this scope
Program04.cpp:38:21: error: âsortDataâ was not declared in this scope
Program04.cpp:39:25: error: âcomputeAveragesâ was not declared in this scope
Program04.cpp:40:19: error: âprintDataâ was not declared in this scope
Program04.cpp: At global scope:
Program04.cpp:45:14: error: variable or field âgetDataâ declared void
Program04.cpp:45:14: error: âStudentTypeâ was not declared in this scope
Program04.cpp:45:38: error: expected primary-expression before âintâ


Everything looks good to me, so i'm obviously not understanding something.
Any suggestions?

************
PROGRAM FILE
************
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using namespace std;
#include "StudentType.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <string>

const int MAX_STDS = 20;

void getData(StudentType[], int&);

void sortData(StudentType[], int&);

void getFormat(string&);

void computeAverages(StudentType[], int&);

void printData(StudentType[], int&);


int main () {

StudentType students[MAX_STDS];
StudentType();

getData(students, n);
sortData(students, n);
computeAverages(students);
printData(students);

return 0;
}

void getData(StudentType students[], int n){
    ifstream fin;
    int grade;
    string filename, name;
    bool done = false;

    cout << "Enter filename: ";
    cin >> filename;
    fin.open(filename.c_str());
    while(true) {
       try {
         fin.open(filename.c_str());
         if(!fin) {
            throw(string("Could not open " + filename + "."));
         }
         break;
       }
       catch (string s) {
          cout << s << endl;
          cout << "Enter a different file name: ";
          cin >> filename;
       }
    }
    n=0;
    while(n<MAX_STDS && getline(fin, name)) {
       students[n].setName(getFormat(name));
       for(int i = 0; i < NUM_GRDS; ++i) {
          fin >> grade;
          student[n].setGrade(grade, i);
       }
       getline(fin, name);
       ++n;
    }
}

void printData(StudentType students[], int n) {
    for(int i = 0; i < n; ++i) {
       students[i].printLine();
    }
}

void computeAverages(StudentType students[], int n) {
    for(int i = 0; i < n; ++i) {
       students[i].computeAverage();
    }
}

void sortData(StudentType students[], int n) {
    for(int i=0; i<n-1; i++) {
       for(int j=0; j < n-1-i; ++j) {
          if(students[j].getName() > students[j+1].getName()) {
             swap(students[j], students[j+1]);
          }
       }
    }
}

void getFormat(string& name) {
    string first;
    string middle;
    string last;
    char n, m;
    int size = 0;
    n = name.find(' ');
    first = name.substr(0, n);
    m = name.find(' ', n + 1);
    size = name.size();
    if (m != string::npos) {
       middle = name.substr(n+1, m-(n+1));
       last = name.substr(m+1, size - (m+1));
    }
    else {
       middle = "";
       last = name.substr(n + 1, size - (n + 1));
    }
    name = last + ", " + first;
    if (middle != "") {
       name = (name + ' ') + middle[0];
    }
}

***********
HEADER FILE
***********
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifdef STUDENTTYPE__H
#define STUDENTTYPE__H

#include <string>
#include<iostream>

const int NUM_GRDS = 10;

class StudentType {
   public:
      StudentType();
      void setName(std::string);
      void setGrade(int, int);
      void computeAverage();
      std::string getName() const;
      void print(std::ostream& = std::cout) const;
   private:
      std::string name;
      int grades[NUM_GRDS];
      float avg;
};

#endif 

*******************
IMPLEMENTATION FILE
*******************
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
#include "StudentType.h"
#include <iomanip>

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

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

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

void StudentType::computeAverage(){
    float total = 0;
    for(int i = 0; i<NUM_GRDS; ++i){
       total += grades[i];
    }
    avg = total/NUM_GRDS;
}

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

void StudentType::printLine(std::ostream& out) const {
    out.setf(std::left);
    out.setf(std::fixed);
    out.setf(std::showpoint);
    out << "\n" << setw(25) << "Student" << setw(50) << "Grades" << setw(10) << "Average" << endl;
    out << "_____________________________________________________________________________________" << endl;
    out << std::left << std::setw(25) << name << std::right << ' ';
    for(int i = 0; i < NUM_GRDS; ++i){
    out << std::setw(5) << grades[i] << ' ';
    }
    out << setprecision(2) << std::setw(6) << avg << endl;
}

**************
My output should look like this after the program compiles...
**************

Enter file name: grades.dat

Student                  Grades                                            Average
________________________________________________________________________________________
Last, First              90   80   70   60   50   40   30   20   10   0    45.00
Last, First              40   40   40   40   40   40   40   40   40   40   40.00
Last, First              54   98   65   32   21   87   54   65   98   32   60.60
Flames, Blood A          9    8    7    6    5    4    3    2    1    0    4.50
bottoms, Car             32   65   98   87   54   24   56   89   78   68   65.10
Guitars, Dean            10   10   10   10   10   10   10   10   10   10   10.00
Honer, Ruth T            78   56   12   23   45   89   31   64   97   79   57.40
Hot, Zepher R            12   54   87   89   56   32   51   46   97   31   55.50
.
.
.



The input file should have this format and include over 20 students for testing purposes:
g0, g1,...g9 should be 10 grades ranging from 0 to 100
First Middle Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9
First Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9
Last edited on
Using namespace even before including the standard libraries?
Yeah, it looks neater to me... lol I usually try to format according to length in descending order. I developed this habit a while ago. I have just started teaching myself about classes and such so it's not in order now, though.
Last edited on
Changed line 1 of StudentType.h from #ifdef STUDENTTYPE__H to #ifndef STUDENTTYPE__H.

Declared int n; below the default constructor StudentType(); within the main function of Program04.cpp

changed 'student[n].setGrade(grade, i);' to "students[n].setGrade(grade, i);' ----- (added the 's' to students)


Deleted & from the parameter of void getFormat(string& name);
Last edited on
Changed Line 16 or the header file from void print(std::ostream& = std::cout) const; TO void printLine(std::ostream& = std::cout) const;

Topic archived. No new replies allowed.