#include "course.h"
int main(){
...
start: cout << "\tEnrollment System College Penang" << endl
<< "-----------------------------------------------------" << endl << endl
<< "1. Enroll a new student into a course" << endl
<< "2. Remove a student from a course " << endl
<< "3. Modify a course detail" << endl
<< "4. Generate grade list" << endl
<< "5. Save to file " << endl << endl
<< "Enter Choice : ";
cin >> choice;
switch( choice ){
case 1:
enrollStudent( theCourse , sizeCourse );
getch();
break;
default:
cout << "Invalid Input ! Please re-enter ! " << endl;
getch();
system( "cls" );
goto start;
}
system( "pause" );
return 0;
}
I recommend doing the same for readRecord(). I.e. having a global void readRecord( ifstream &inFile ,Course *theCourse, int &sizeCourse )
for all courses and a class member void readRecord( ifstream &inFile )
to read the course details. Which would greatly improve the legibility of your code
and i feel that when i pass the parameter isn't wrong already?
i saw that u using only void Course :: enrollStudent()
without passing any parameter . but i think they should pass a numOfCourse ?
of am i declaring in wrong way already ?
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Course::readRecord(class std::basic_ifstream<char,struct std::char_traits<char> > &,int &)" (?readRecord@Course@@QAEXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@AAH@Z) referenced in function _main
this my error function?
okay. but i having an error in my readfunction()
it's say my theCourse is using without been initialized . thats why i having the problem of my course . if i put my [b]readRecord()[/code] in my class function . how should i change inside my code?
void Course :: enrollStudent( int& numOfCourse ){
Course *theCourse;bool found = 0;
int foundIndex = 0 ;
int choice = 0;
cout << "0.Exit " << endl;
for( int i = 0 ; i < numOfCourse ; i++ ){
cout << i << "." << theCourse[i].courseCode << " " << theCourse[i].courseName << endl;
}
...
In bold, you can see where you create a pointer and then you start using it, but you never made it point at anything! It's juts some random pointer. What is it meant to be pointing at? If this is meant to be a class function, belong to the Course class, then it's already inside a Course object. You can just use the object variables:
1 2 3 4 5 6 7 8 9 10 11 12
private:
string courseCode;
string courseName;
int credit;
int numOfStudent;
public:
struct Student{
string studentID;
string studentName;
double studentMarks;
}theStudent[30];
i already make it point to theCourse[i].courseCode and the courseName already . from my cout . but i having problem when readFunction(). it's say my theCourse is being use without initialized
i already make it point to theCourse[i].courseCode
I disagree. Look at the code.
1 2 3 4 5 6 7 8 9 10 11
void Course :: enrollStudent( int& numOfCourse ){
Course *theCourse;bool found = 0;
int foundIndex = 0 ;
int choice = 0;
cout << "0.Exit " << endl;
for( int i = 0 ; i < numOfCourse ; i++ ){
cout << i << "." << theCourse[i].courseCode << " " << theCourse[i].courseName << endl;
}
The first line in bold is where you create the pointer named theCourse. The second line in bold is where you try to use what it is pointing at. Which line of code between those two do you think is setting the pointer to point at something?
The pointer that you set in main.cpp, like this: theCourse = new Course[numOfCourse];//dynamic array created does not exist inside the functions. The variables that exist inside class functions are:
1) variables you pass in as parameters
2) variables you create in the function
3) class variables that are part of the class
4) Global variables
You are creating a brand new pointer named theCourse in the functions, and it is not being set to point at anything.
void Course :: enrollStudent( int& numOfCourse , Course *theCourse ){
bool found = 0;
int foundIndex = 0 ;
int choice = 0;
cout << "0.Exit " << endl;
for( int i = 0 ; i < numOfCourse ; i++ ){
cout << (i+1) << "." << theCourse[i].courseCode << " " << theCourse[i].courseName << endl;
}
cout << "Enter Your Choice : ";
cin >> choice;
if( choice > 0 && choice < numOfCourse )
theCourse[choice - 1].enrollStudent( numOfCourse , theCourse );
elseif(choice != 0)
cout << "Invalid Input ! " << endl;
cin.ignore();
do{
cout << "Enter student name : ";
getline( cin,theStudent[numOfStudent].studentName );
cout << "Enter student's mark : ";
double studentMarks;
cin >> theStudent[numOfStudent].studentMarks;
numOfStudent++;
if( numOfStudent > 30 )
cout << " The number of student enrolled for this course are full ! " << endl;//Display error message if maximum student reached
}while( numOfStudent < 31 );//Do checking on the number of student that enroll for the course
}
from the code we can know that if we press the choice 1 . the function will be recall back . so it will keep call user to input the choice . so what's the advise can give ?
void Course :: readRecord( ifstream &inFile , int &sizeCourse , Course *theCourse ){
string SubjectCode;
string SubjectName;
string StudentName;
int theCredit = 0;
int sizeOfStudent = 0;
string filename;
if ( inFile.is_open() ){
inFile.ignore();
for( int i = 0 ; i < sizeCourse ; i++ ){
getline( inFile , SubjectCode );
theCourse[i].setCourseCode( SubjectCode );
getline( inFile , SubjectName );
theCourse[i].setCourseName( SubjectName );
inFile >> theCredit;
theCourse[i].setCredit( theCredit);
inFile >> sizeOfStudent;
theCourse[i].setNumOfStudent( sizeOfStudent );
inFile.ignore();
for ( int z = 0 ; z < sizeOfStudent ; z++ ){
getline( inFile , theCourse[i].theStudent[z].studentName);
inFile >> theCourse[i].theStudent[z].studentMarks;
inFile.ignore();
}
}
}
cout << sizeCourse << endl;
for( int i = 0 ; i < sizeCourse ; i++ ){
cout << theCourse[i].getCourseCode() << endl
<< theCourse[i].getCourseName() << endl
<< theCourse[i].getCredit() << endl
<< theCourse[i].getNumOfStudent() << endl;
for( int z = 0 ; z < theCourse[i].getNumOfStudent() ; z++ ){
cout << theCourse[i].theStudent[z].studentName << endl
<< theCourse[i].theStudent[z].studentMarks<< endl;
}
}
}
void Course::enrollStudent(){ // This is the class member
bool found = 0;
int foundIndex = 0 ;
do{
cin.ignore();
cout << "Enter student name : ";
getline(cin, theStudent[numOfStudent].studentName );
cout << "Enter student's mark : ";
double studentMarks;
cin >> theStudent[numOfStudent].studentMarks;
numOfStudent++;
if( numOfStudent > 30 )
cout << " The number of student enrolled for this course are full ! " << endl;//Display error message if maximum student reached
}while( numOfStudent < 31 );//Do checking on the number of student that enroll for the course
}
void enrollStudent( Course *theCourse , int& numOfCourse ){
bool found = 0;
int foundIndex = 0 ;
int choice = 0;
cout << "0.Exit " << endl;
for( int i = 0 ; i < numOfCourse ; i++ ){
cout << (i+1) << "." << theCourse[i].getCourseCode() << " " << theCourse[i].getCourseName() << endl;
cout << "Enter Your Choice : ";
cin >> choice;
if( choice > 0 && choice < numOfCourse )
theCourse[choice - 1].enrollStudent();
elseif(choice != 0)
cout << "Invalid Input ! " << endl;
}
got a senior told me , my readRecord is in wrong state already . isn't?
do my readRecord() should be in member of class , of should it declare as a outside function ? because last time u told me that i only should have 1 parameter which is ifstream &
but then now my read function is have 3 parameters. isn't wrong ?
got a senior told me , my readRecord is in wrong state already . isn't?
do my readRecord() should be in member of class , of should it declare as a outside function ? because last time u told me that i only should have 1 parameter which is ifstream &
but then now my read function is have 3 parameters. isn't wrong ?
No, I didn't say that it's wrong. it could be improved, but it's not necessary. (The same scheme like enrollStudent(), you just need to grasp why enrollStudent() is split this way.)
The loop on line 51 is wrong. numOfStudent must be < 30 otherwise it's out of bounds. This loop will force the user to enter 31 students until he gets an error message
if( numOfStudent < 31 ){
cin.ignore();
cout << "Enter student name : ";
getline(cin, theStudent[numOfStudent].studentName );
cout << "Enter student's mark : ";
cin >> theStudent[numOfStudent].studentMarks;
numOfStudent++;
}
elseif( numOfStudent > 30 )
cout << " The number of student enrolled for this course are full ! " << endl;//Display error message if maximum student reached
now for my enroll student.
after i enroll the newstudent.
i go back to my readRecord() . but then . now the problem is . while i read back my file. it's output is like this