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().
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
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.
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 numOfCourse = 0;
int choice = 0;
// Open the file here.
// Ask the user for the file name here, not in your function.
// 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 wil store line 2 to 9 then theCourse wil store the data from line 10 to 19 . Did it clear? Sorry