sementation fault[g++]

Hello.
I am to write a program that assigns to each professor(which I sometimes call supervisor) a pupil and a project to each pupil.Here is the code for "supervisor" class:
class SUPV {

public:

SUPV( const char szname[20], const char szlastName[20], const char szpersonalCode[10] );


char* getPersonalCode();
void setName( char name[] );
void setLastName( char lName[] );
void setHomeAddress( char HA[] );
void setPersonalCode( char pCode[] );
void setPhoneNumber( char number[] );

void ediProject();

void allocateProject( Student& std);

void assessProject( Student& );

short superviseStudent( Student& objStudent);
void addProject();
void setMaxStd( short imax );
void edit();
void report();

Student objPupil;

Project objProject;
list<Student> lstStudent;

list<Project> lstProject;

private:

char name[20];

char lastName[20];

char personalCode[10];

char homeAddress[100];

char phoneNumber[10];

short maxStd;
};

and this is how the constructor is defined:
SUPV::SUPV(const char szname[20], const char szlastName[20], const char szpersonalCode[10])

{
cout << "SUPV ctor" << endl;

for( int i = 0;i < 20;++i )

{
name[i] = szname[i];

lastName[i] = szlastName[i];
}

for( int j = 0;j < 20;++j )

personalCode[j] = szpersonalCode[j];



}

ut when I run the program, I get a "segmentation fault erre at the runtime.no syntax error.).This is a portion of the Main() function.the sourcess of the error are marked in this:


int main()

{




char pfName[20] = {0};

char pfLastName[20] = {0};

char pfCode[10] = {0};

char pfAddr[100] = {0};

char pfPhone[10] = {0};
char title[100] = {0};
char stdName[20] = {0};
char stdLastName[20] = {0};

char prName[30] = { 0 };
char oldStdID[10] = {0};
short stdProjectGrade;

short pfmaxstd;

int option;



cout << "*********************Menu*************************************************"
"\nSelect an option:\n 1)Recruit a professor\n 2)Enroll a student" <<
"\n3)add a project \n 4)Edit an student's information\n5)Edit a project's information"
<<"\n 6)edit a professor's info\n7)Report on a professor" << endl;
cin >>option;





switch(option){
case 1:
{
system("clear");
cout << "***********************Recruiting a professor********************************\n\n\n\n" <<endl;
cout << "Name is:" << pfName << " Lastname is: "<< pfLastName << endl;
cout << "Enter Professor's Name and LastName(seperated by spaces)" << endl;

cin >> pfName >> pfLastName;

cout << "Enter Professor's personal code:" << endl;

cin >> pfCode;

cout << "here" << endl;

SUPV *Professor = new SUPV( pfName, pfLastName, pfCode );// !!SEGMENTATION FAULT!!



// supervisorList.push_back(*Professor);

}
break;
case 2:
{
system("clear");
cout << "Enter student's name and last name(seperated by space):" << endl;

cin >> stdName >> stdLastName;


cout << "here" << endl;
Student *objPupil = new Student(stdName, stdLastName);// !!SEGMENTATION FAULT!!



// studentList.push_back(*objPupil);
}

break;
One thing I noticed that may cause the problem is inside your SUPV constructor:

1
2
3
4
5
//...
for( int j = 0;j < 20;++j ) //<- this should be: for( int j = 0;j < 10;++j )

personalCode[j] = szpersonalCode[j];
//... 

Perhaps there's something like this in your Student constructor too. Check it!
Last edited on
Thank you master.but it still seg-fault
Last edited on
You checked both your SUPV and Student constructors? And they're just fine?

Then I'd recommend using the strings library in place of char arrays, if you're permitted, of course. It might fix a few problems and they're much easier to mess around with.
http://cplusplus.com/reference/string/string/

Reason why: Segmentation faults occur when a program tries to allocate memory that it cannot access, write to memory it cannot access, or read from memory it cannot access. Something tells me that there's something wrong with those character arrays...

-Albatross
Last edited on
You might want to consider using std::string rather than char[10] to store your data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <string>

class SUPV
{

public:

	SUPV( const std::string& szname, const std::string& szlastName, const std::string& szpersonalCode );


	std::string getPersonalCode();
	void setName(const std::string& name);
	void setLastName(const std::string& lName );
	void setHomeAddress(const std::string& HA); 
	void setPersonalCode(const std::string& pCode);
	void setPhoneNumber(const std::string& number);	
	
	...

};
Last edited on
Topic archived. No new replies allowed.