declaring problem.

I am struggling with finishing this program. it says that "studentType" is not declared. well its suppose to be "studentType studentList". any help would be greatly apprecaited.

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

using namespace std;

const int MAX_NO_OF_STUDENTS = 10;

void getStudentData (ifstream& infile,
                     studentType studentList[],
                     int numberOfStudents);
                     
void printGradereports (ofstream& outfile,
                       studentType studentList[],
                       int numberOfStudents,
                       double tuitionRate);

int main()
{
    studentType studentLists [MAX_NO_OF_STUDENTS];
    
    int noOfStudents;
    double tuitionRate;
    ifstream infile;
    ofstream outfile;
    
    infile.open ("stData.txt");
    
    if (!infile)
    {
        cout << "The input file does not exist. "
             << "Program terminates." << endl;
        return 1;
    }
    
    outfile.open ("stDataOut.txt");
    
    infile >> noOfStudents;
    infile >> tuitionRate;
    
    getStudentData (infile, studentList, noOfStudents);
    printGradeReports (outfile, studentList,
                       noOfStudents, tuitionRate);
    return 0;
}
Last edited on
Show us where studentType is defined; presumably within studentType.h?
studentType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef H_studentType
#define H_studentType

#include <fstream>
#include <string>
#include "personType.h"
#include "courseType.h"

using namespace std;




#endif 


studentType.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "courseType.h"
#include "personType.h"
#include "studentType.h"

using namespace std;


courseType and personType are the same.
Last edited on
er... studentType isn't declared anywhere in that code you pasted.

You must have a studentType class somewhere, right? Where is it?
studentType.h
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
#ifndef H_studentType
#define H_studentType

#include <fstream>
#include <string>
#include "personType.h"
#include "courseType.h"

using namespace std;

class studentType: public personType
{
      public:
             void setInfo (string fName, string lName, int ID,
                           int nOfCourses, bool isTPaid,
                           courseType courses[], char courseGrades[]);
             
             void print (ostream& outF, double tuitionrate);
             
             studentType ();
             
             int getHoursEnrolled ();
             
             double getGpa ();
             
             double billingAmount (double tuitionRate);
             
      private:
              void sortCourses ();
              
              int sId;
              int numberOfCourses;
              
              bool isTuitionPaid;
              
              courseType courseEnrolled[6];
              char coursesGrade[6];
};

             


#endif 


studentType.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "courseType.h"
#include "personType.h"
#include "studentType.h"

using namespace std;

void studentType::setInfo (string fName, string lName, int ID,
                          int nOfCourses, bool isTPaid,
                          courseType courses[], char cGrades[])

{
     int i;

     setName (fName, lName);
     
     sId = ID;
     isTuitionPaid = isTPaid;
     numberOfCourses = nOfCourses;
     
     for (i = 0; i < numberOfCourses; i++)
     {
         coursesEnrolled[i] = courses[i];
         coursesGrade[i] = cGrades[i];
     }
     
     sortCourses();
}

studentType::studentType()
{
     numberOfCourses = 0;
     sId = 0;
     isTuitionPaid = false;
     
     for (int i = 0; i < 6; i++)
          coursesGrade[i] = '*';
}

void studentType::print (ostream& outF, double tuitionRate)
{
     int i;
     
     outF << "Student Name: " << getFirstName()
          << "  " << getLastName() << endl;
          
     outF << "Student ID: " << sId << endl;
     
     outF << "Number of courses enrolled: "
          << numberOfCourses << endl;
     outF << endl;
     
     outF << left;
     outF << "Course No" << setw(15) << "  Course Name"
          << setw(8) << "Credits"
          << setw(6) << "Grade" << endl;
     
     outF << right;
     for (i = 0; i < numberOfCourses; i++)
     {
         coursesEnrolled[i].print(outF);
         
         if (isTuitionPaid)
            outF << setw(4) << coursesGrade[i] << endl;
         else
            outF << setw(4) << "***" << endl;
     }
     outF << endl;
     
     outF << "Total number of credit hours: "
          << getHoursEnrolled() << endl;
          
     outF << fixed << showpoint << setprecision(2);
     
     if (isTuitionPaid)
         outF << "Mid-Semester GPA: " << getGpa()
              << endl;
     else
     {
         outF << "*** Grades are being held for not paying "
              << "the tution, ***" << endl;
         outF << "Amount Due: $" << billingAmount(tutionrate)
              << endl;
     }               

     outF << "_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"
          << "_*_*_*_*" << endl << endl;
}

int studentType::getHoursEnrolled()
{
    int totalCredits = 0;
    int i;
    
    for (i = 0; i < numberOfCourses; i++)
        totalCredits += coursesEnrolled[i].getCredits();
        
    return totalCredits;
}

double studentType::billingAmount(double tuitionrate)
{
       return tuitionRate * getHoursEnrolled();
}

double studentType::getGpa()
{
       int i;
       double sum = 0.0;
       
       for (i = 0; i < numberOfCourses; i++)
       {
           switch (courseGrade[i])
           {
           case 'A':
                sum += coursesEnrolled[i].getCredits() * 4;
                break;
           case 'B':
                sum += coursesEnrolled[i].getCredits() * 3;
                break;
           case 'C':
                sum += coursesEnrolled[i].getCredits() * 2;
                break;
           case 'D':
                sum += coursesEnrolled[i].getCredits() * 1;
                break;
           case 'F':
                break;
           default:
                cout << "Invalid Course Grade." << endl;
           }
       }
       
       return sum / getHoursEnrolled();
}

void studentType::sortCourses()
{
     int i, j;
     int minIndex;
     courseType temp;
     char tempGrade;
     string course1;
     string course2;
     
     for (i = 0; i < numberOfCourses - 1; i++)
     {
         minIndex = i;
         
         for (j = i + 1; j < numberOfCourses; j++)
         {
             
             course1 =
              coursesEnrolled[minIndex].getCourseNumber();
             course2 = courseEnrolled[j].getCourseNumber();
             
             if (course1 > course2)
                minIndex = j;
         }
         
         temp = coursesEnrolled[minIndex];
         coursesEnrolled[minIndex] = coursesEnrolled[i];
         coursesEnrolled[i] = temp;
         
         tempGrade = coursesGrade[minIndex];
         coursesGrade[minIndex] = CoursesGrade[i];
         coursesGrade[i] = tempGrade;
     }
}


studentType studentList[MAX_NO_OF_STUDENTS];

int noOfStudents;
double tuitionRate;

ifstream infile;
ofstream outfile;

void getStudentData (ifstream& infile,
                    studentType studentList[],
                    int numberOfStudents)
{

     string fName;
     string lName;
     int ID;
     int noOfCourses;
     char isPaid;
     
     bool
     isTuitionPaid;
     

     string cName;
     string cNo;
     int credits;
     
     int count;
     int i;
     
     courseType course[6];
     
     char cGrades[6];
     
     for (count = 0; count < numberOfStudents; count++)
     {
         infile >> fName >> lName >> ID >> isPaid;
         
         if (ispaid == 'Y')
            isTuitionPaid = true;
         else
            isTuitionPaid = false;
         
         infile >> noOfCourses;
         
         for (i = 0; i < numberOfCourses; i++)
         {
             infile >> cName >> cNo >> credits
                    >> cGrades[i];
             courses[i].setCourseInfo(cName, cNo, credits);
             
         }
         sudentList[count].setInfo(fName, lName, ID, noOfCourses,
                                   isTuitionPaid, courses, cGrades);
     }
}

void printGradesReports(ofstream& outfile, studentType studentList[],
                        int numberOfStudents,
                        double tuitionRate)
{
     int count;
     for (count = 0; count < numberOfStudents; count++)
         studentList[count].print(outfile, tuitionRate);
}


is that looking more correct? but now when I compile i get a error at #include "studentType.h".
but now when I compile i get a error at #include "studentType.h".


What's the error.
4 C:\main.cpp In file included from C:\main.cpp

12 C:\studentType.h expected class-name before '{' token

C:\main.cpp In function `int main()':

42 C:\main.cpp `studentList' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

44 C:\main.cpp `printGradeReports' undeclared (first use this function)

These are the errors i am getting.
Last edited on
Sounds like personType is #including studenttype when it shouldn't be.

You can't just have all your headers include each other. They should only include what they need.

See sections 4 and 5 of this article:

http://cplusplus.com/forum/articles/10627/#msg49679

Fix your headers so they all include things the "right way", then these errors should disappear.

I feel I'm not getting the diff between .h and .cpp. what goes where and everything. I know the class terms goes into .h text doc. but as far as what goes in the .cpp to calborate with it. no idea.
A very general rule of thumb:


Things that go in headers:
-----------------------
classes
structs
function prototypes
typedefs
enums
templates


Things that go in source (cpp) files:
-----------------------
function bodies



The idea is, headers provide a simple interface for which you can write your program. Whereas source files contain the actual code that does stuff.
thank you so much for all you help Disch! really apprecaite it. Sunday I will be tinckering more with it. Your last post shoulod help me figure out what goes where.
In addition, it is better to use .h file like a reference and source... just imagine like a connector of main.cpp and student.cpp

declare you main fuction in main.cpp and declare your class functions in student .cpp and mention in all of them your .h file and also mention all of them ONLY in .h file not each other.
Topic archived. No new replies allowed.