read file problem

Pages: 123
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:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <string>
#include <iostream>
using namespace 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.



Last edited on
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.
Last edited on
@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 < 31 30 ){
...
	}
	else if( 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.
my all current code
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
#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 readRecord( ifstream & , int & , Course * );
	void enrollStudent();
	void RemoveStudent( int & );
	void ModifyCourse( int & );
	void GradeList( int & );
	void writeRecord(  int & );
};

void enrollStudent( Course * , int & );


course.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 ;

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

		numOfStudent++;
	}
}

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();
	else if(choice != 0)
			cout << "Invalid Input ! " << endl;
		
}


and main.cpp
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
#include "course.h"

int main(){
	
	string filename;
	Course *theCourse;
	int numOfCourse = 0;
	int choice = 0;
	ifstream inFile;

start1:	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

	theCourse->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
			 << "Enter Choice : ";

	cin  >> choice;

	switch( choice ){
	case 1:
		enrollStudent( theCourse , numOfCourse );
		getch();
		goto start1;
		break;
	default:
		cout << "Invalid Input ! Please re-enter ! " << endl;
		getch();
		system( "cls" );
		goto start;
	}

	system( "pause" );
	return 0;
}
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  

}
here is my current 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
54
55
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?
Yes, remove line 48/49. Move line 4 just before line 26. So that you read the file just once.

Btw: when the user enters 'q' or 'Q' the user will see "Invalid Input ! Please re-enter ! ". Better change that.
Changed. So if I want to check what my memory current stored. Can I use back the part of code that at read record? Which show out the information 1
I'm not sure what you mean. If you want only a part of a function, break that said function into two:
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
void showCourses( int &sizeCourse , Course *theCourse );

void 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();
			}
		}
	}
	showCourses(sizeCourse , theCourse );
}

void showCourses( int sizeCourse , Course *theCourse ){

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


Probably I won't be around here for the next couple of days (approx. a week). So sorry, I may not be able to answer your questions during this time
Topic archived. No new replies allowed.
Pages: 123