void readRecord( Course *theCourse , int &sizeCourse ){
string SubjectCode;
string SubjectName;
string StudentName;
int theCredit;
int sizeOfStudent;
string filename;
ifstream inFile;
cin.ignore();
cout << "Enter filename to be read : ";
getline( cin , filename );
inFile.open( filename.c_str());
if( inFile.fail() ){
cout << "Unable to open file ! Re-check !" << endl;
getch();
exit(1);
}
if ( inFile.is_open() ){
inFile >> sizeCourse;
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);
for ( int z = 0 ; z < sizeOfStudent ; z++ ){
getline( inFile , theCourse[i].theStudent[z].studentName);
inFile >> theCourse[i].theStudent[z].studentMarks;
}
}
}
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;
}
}
}
my logical concept aren't true? my output for the structure is alien words. and i cant loop for the second CourseName ; it's remain the APPLIED PROGRAMMING
This problem is probably being caused because you are mixing getline() and the extraction operator. Remember anytime you use the extraction operator to extract a number the end of line character is left in the input buffer, and when you then go to extract with the getline() you will only extract this end of line character, and because you didn't actually extract the character information your next use of the extraction operator for the number will fail. You need to add a inFile.ignore() before your getline() on line 40 of your first post, it should be inside the loop, but before the getline().
my question. why i must declare the const INT SIZE . if i comment them out.
my program got error.
i want to set my dynamic array for course after return the size of sizeCourse then only create it
I strongly recommend that you use the std::vector instead of the arrays.
Now part of your problem is that you are trying to allocate different amounts of memory to the same variable. In the above code you have allocated memory for 30 of your class with the following
1 2
constint SIZE = 30;
theCourse = new Course[SIZE];
Then you use this variable when you read the file.
When you return from reading the file you try to allocate a different amount of memory to this variable.
1 2
int sizeCourse = readRecord( theCourse , numOfCourse );
theCourse = new Course[sizeCourse];//dynamic array
This is wrong is several ways, the first is that you are creating a memory leak by not deleting the memory you allocated, before you try to allocate this new block.
Secondly it really looks like you want to read only the first line of your file to determine how many instances are contained in your file. Then after you read this one line you allocate memory for your array, don't allocate memory before you read this first line. Then after you have allocated the memory for your array, read the entire file into your array.
First don't allocate memory for this array before you read the first line of the file, this line contains the information as to how many records are in the file. Then use the value contained in the first line to allocate memory for this array before you try to read the file in readRecord(). You will need to modify readRecord() because you will have already read the first line, don't try to read this first line again. Don't try to allocate memory after the call to readRecord().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Change your function to allow passing an open file stream.
void readRecord(ifstream& inFile, Course *theCourse , int &sizeCourse );
int main(){
Course *theCourse;
int numOfCourse = 0;
int choice = 0;
// Open the file here.
// Ask the user for the file name here, not in your function.
ifstream fin("YourFileName");
// Insure the file opened correctly
// read the first line.
fin >> numOfCourse;
// Make sure numOfCourse is greater than zero.
// Now allocate the memory for your array.
theCourse = new Course[numOfCourse];
int sizeCourse = readRecord(fin, theCourse , numOfCourse );
Then you will need to modify your function to read the entire file instead of just one record. You won't need to open your file inside the function, since you have already opened it in main. You will need some kind of loop to read the entire file.
typo error. I already post my current code? For my course. A course got maximum 30 student can be enrolled. Now. See my text file. The 2 mean two course. So. When I infile . I get the size course. So each course have the own function. That I implement inside my class. So. How I gonna to read them and store the data to each course? Like my text file. Line 2 to line 9 is 1 course. Line 10 to line 19 is other course. So when I read the record theCourse[0] wil store line 2 to 9 then theCourse[1] wil store the data from line 10 to 19 . Did it clear? Sorry
#include "course.h"
int main(){
string filename;
Course *theCourse;
int numOfCourse = 0;
int numOfStudent = 0;
int choice = 0;
ifstream inFile;
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, theCourse, numOfCourse );
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
<< "Enter Choice : ";
cin >> choice;
switch( choice ){
case 1:
theCourse->enrollStudent();
getch();
break;
default:
cout << "Invalid Input ! Please re-enter ! " << endl;
getch();
system( "cls" );
goto start;
}
system( "pause" );
return 0;
}
how should i call the enrollStudent() function? because from my txt file i got two course right? . so should i let user choose which course he want to enroll ? example