Maybe you are making it more complicated than it has to be? Do you need to make a student able to enroll, or perhaps all you have to do is read from the file?
I would say your Student and Course are too intertwined. I also think that Course should not read an entire file during readRecord, rather it just reads for that course. Here would at least solve your first and last posts:
#include <string>
#include <iostream>
usingnamespace std;
class Student
{
string name;
int score;
public:
Student(){}
bool read(istream& in = cin)
{
if (in == cin) cout << "Please enter the name: ";
getline(in, name);
if (in == cin) cout << "Please enter the score: ";
in >> score;
return in.good();
}
void print(ostream& out = cout)
{
out << "Name: " << name << " Score: " << score << endl;
}
};
#define MAXSTUDENT 30
class Course
{
string id;
string name;
int credits;
int amtStudents;
Student *students;
public:
Course(void) {}
~Course(void) { delete [] students; }
bool read(istream& in = cin)
{
// do if for if in == cin
istream >> id >> name >> credits >> amtStudents;
// if amtStudents > MAXSTUDENTS || amtStudents == 0 there are problems
students = new Student[amtStudents];
int i;
for (i = 0; i < amtStudents && students[i].read(in); i++)
;
return i == amtStudents;
}
void print(ostream& out = cout)
{
out >> id >> endl >> name >> endl >> credits >> endl;
for (i = 0; i < amtStudents; i++) students[i].print(out);
}
};
int main(void)
{
Course* courses;
ifstream input("courselist.txt");
int numCourses;
input >> numCourses;
courses = new Course[numCourses];
for (int i = 0; i < numCourses; i++)
{
if (!courses[i].read(input);
{
cout << "Error reading\n";
break;
}
}
// perhaps see if you are at eof?
input.close();
cout << "This is what I read:\n";
for (int i = 0; i < numCourses; i++)
courses[i].print();
delete [] courses;
return 0;
}
Havent tested any of that, might be various syntax errors.
for my question . i have to do for the read from file . and then student is able to enroll . and then save back for the file . i got 2 course .each course maximum 30 students is able to enroll .
i should read this from txt file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2
CSCI124
Applied Programming
6
2
Alice
56
Bob
75
CSCI114
Procedural Programming
6
3
Nancy
89
Paul
67
Lisa
58
and this my question A Course object should be able to perform the following functions:
Enroll a new student – this will involve getting the student’s name and mark from the user. There should be a checking on the number of students already enrolled so far in the course. If the course is full, no new enrollment should be allowed. Number of students enrolled should be updated accordingly.
@code777 . if you see any message . please see page 2 last post . that's my error i having.
@LowestOne
Yes, similar. But it's ok to have a fixed size array of students for enroll. It's somewhat overdo.
@Felicia123
i using if else statement to do it
No, you must understand that an array starts with 0. theStudent has a capacy of 30 hence the index goes form 0 to 29. Everything else is out of bounds!
1 2 3 4 5
if( numOfStudent < 3130 ){
...
}
elseif( numOfStudent > 30 ) // Not necessary. The if said it all
cout << " The number of student enrolled for this course are full ! " << endl;//Display error message if maximum student reached
i go back to my readRecord() . but then . now the problem is . while i read back my file. it's output is like this
Where does this output come from (i.e. show the code)?
It looks as if the file cannot be opened
Btw: you need to write back the data after enrolled (i.e. your need writeRecord())otherwise readRecord() overwrites the data.
oh dear, you really don't know what this talking about member functions is all about, do you?
No, readRecord( ifstream &inFile , int &sizeCourse , Course *theCourse ) is not a member function of Course
The reason why you get that output is that you continue after cout << "Unable to open file !" << endl;. You should either exit() the program or ask in a loop for a better filename. That's the problem. The system cannot open the file with the name you provided. The code looks ok so far.
Note:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void Course::enrollStudent(){ // This is the class member
bool found = 0;
int foundIndex = 0 ;
if( numOfStudent < 30 ){
cin.ignore();
cout << "Enter student name : ";
getline(cin, theStudent[numOfStudent].studentName );
cout << "Enter student's mark : ";
cin >> theStudent[numOfStudent].studentMarks;
numOfStudent++;
}
else // The 'if' is not necessary here. The 'else' remains!
cout << " The number of student enrolled for this course are full ! " << endl;//Display error message if maximum student reached
}
int main(){
char choice = '0';
do{
string filename;
Course *theCourse;
int numOfCourse = 0;
ifstream inFile;
cin.ignore();
cout << "Enter filename to read : ";
getline( cin , filename );
cout << endl;
inFile.open( filename.c_str() );
if( inFile.fail() )
cout << "Unable to open file !" << endl;
inFile >> numOfCourse;
theCourse = new Course[numOfCourse];//dynamic array created
readRecord( inFile, numOfCourse , theCourse );
getch();
system( "cls" );
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
<< "6. Press q or Q to exit the program !" << endl
<< "Enter Choice : ";
cin >> choice;
switch( choice ){
case'1':
enrollStudent( theCourse , numOfCourse );
getch();
break;
default:
cout << "Invalid Input ! Please re-enter ! " << endl;
getch();
system( "cls" );
goto start;
}
}while( choice != 'q' || choice !='Q' );
system( "pause" );
return 0;
}
after i enroll a student. it can loop back . but the when i read back for the second loop . the new student that i enroll didn't store the memory into the program?