read file problem

Pages: 123
i wan to read this txt
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 my currently implementation coding:
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
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
Please show how you have defined your variable theCourse. How much memory did you allocate to this variable?


my course.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
class Course{
private:
	string courseCode;
	string courseName;
	int credit;
	int numOfStudent;
public:

	struct Student{
		string studentID;
		string studentName;
		double studentMarks;
	}theStudent[30];

	Course();
	

	void setCourseCode( string );
	string getCourseCode();

	void setCourseName( string );
	string getCourseName();

	void setCredit( int );
	int getCredit();

	void setNumOfStudent( int );
	int getNumOfStudent();

	void setStudentID( string );
	string getStudentID();

	void setStudentMarks( double );
	double getStudentMarks();
};
and main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
	const int SIZE = 30;
	Course *theCourse;
	theCourse = new Course[SIZE];//dynamic array
	int numOfCourse = 0;
	int choice = 0;

switch( choice ){
	case 1:
		readRecord( theCourse , numOfCourse );
		//enrollStudent( theCourse , numOfCourse );
		getch();
		break;
Last edited on
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 current main.cpp
1
2
3
4
5
6
7
8
9
int main(){
	
	Course *theCourse;
	const int SIZE = 30;
	theCourse = new Course[SIZE]; 
	int numOfCourse = 0;
	int choice = 0;
	int sizeCourse = readRecord( theCourse , numOfCourse );
	theCourse = new Course[sizeCourse];//dynamic array 
my read function()
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
int 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);
			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;
			 }
	}
	return sizeCourse;
}
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
any senior gv a hlp?
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
	const int 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.
ya i know im wrong for it .

and i did not know much about vector. that's why I'm not gonna to use it .

@jib. mind to provide for me ?
1
2
int sizeCourse = readRecord( theCourse , numOfCourse );
	theCourse = new Course[sizeCourse];//dynamic array   

how should i declare the
Course *theCourse without the error ?
and then theCourse[ can pass the value to readRecord() function?
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.

Last edited on
what i doing until now
my course.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
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

class Course{
private:
	string courseCode;
	string courseName;
	int credit;
	int numOfStudent;
public:

	struct Student{
		string studentID;
		string studentName;
		double studentMarks;
	}theStudent[30];

	Course();
	
	void setCourseCode( string );
	string getCourseCode();

	void setCourseName( string );
	string getCourseName();

	void setCredit( int );
	int getCredit();

	void setNumOfStudent( int );
	int getNumOfStudent();

	void setStudentID( string );
	string getStudentID();

	void setStudentMarks( double );
	double getStudentMarks();

	void enrollStudent( int & , int &);
	void RemoveStudent( int & );
	void ModifyCourse( int & );
	void GradeList( int & );
	void writeRecord(  int & );
};

void readRecord( ifstream & ,Course *, int & );



here the txt file i read:
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


my readrecord()
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
void readRecord( ifstream &inFile ,Course *theCourse, int &sizeCourse ){

	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;
			 }
	}
}


for the enrollStudent function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void enrollStudent( Course *theCourse , int &sizeCourse , int &sizeOfStudent ){

	bool found = 0;
	int foundIndex = 0 ;

	cin.ignore();
	cout << "Enter student name : ";
	getline(cin,theCourse[sizeCourse].theStudent[sizeOfStudent].studentName );
	
	cout << "Enter student's mark : ";
	double studentMarks;
	cin  >> theCourse[sizeCourse].theStudent[sizeOfStudent].studentMarks;

}


how should i insert the variable fpr the name and marks? their size.. according to the course size i read and student
Last edited on
wao. i also want to learn this
any people mind to teach?
how should i insert the variable fpr the name and marks? their size.. according to the course size i read and student
What is fpr?


Post your current code, then ask specific questions based on the code provided.


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
??
I'm not sure what your problem is?

I'd think that the enrollStudent function is supposed to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void enrollStudent( Course *theCourse , int sizeCourse ){

	bool found = 0;
	int foundIndex = 0 ;

	cin.ignore();
	cout << "Enter student name : ";
	getline(cin,theCourse[sizeCourse].theStudent[theCourse[sizeCourse].numOfStudent].studentName );
	
	cout << "Enter student's mark : ";
	double studentMarks;
	cin  >> theCourse[sizeCourse].theStudent[theCourse[sizeCourse].numOfStudent].studentMarks;

	theCourse[sizeCourse].numOfStudent++;

}
But you have already a member function in your class Course. That would make it easier:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Course::enrollStudent(){

	bool found = 0;
	int foundIndex = 0 ;

	cin.ignore();
	cout << "Enter student name : ";
	getline(cin, theStudent[numOfStudent].studentName );
	
	cout << "Enter student's mark : ";
	double studentMarks;
	cin  >> theStudent[numOfStudent].studentMarks;

	numOfStudent++;

}

...

theCourse[i].enrollStudent();
@code777

theCourse[i].enrollStudent();

is the code that call in main?

for my main
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
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int choice = 0;

cout << "1. CSCI124 Applied Programming " << endl;
cout << "2.CSCI114 Procedural Programming " << endl << endl;
cout << "Enter Your choice : ";
cin   >> choice;

switch(choice){
case 1:
         theCourse[0].readRecord();
         break;
case 2:
         theCourse[1].readRecord();
         break;
default:
         cout << "Invalid Input ! " << endl;
         
}


Should i do it at the start place? or ?
Pages: 123