well I have a class of type student. Each student has two data members: a name ( char pointer ) and a gpa (double ). In main I create a array of type student able to hold 10 students. After filling it, I decide to test out my copy constructor, but creating a new student and giving it the value of one of the array students. However, the name of the new student ends up containing garbage even tho the student name in the array was sized exactly!
I decided to overload my assignment operator for copying but i'll start by showing my copy constructor:
student ( const student& rhs )
{
int len;
len = strLen(rhs.name);
name = new char[ len ];
gpa = rhs.gpa;
strCpy( rhs.name, name );
int len;
len = strLen(rhs.name);
if( rhs.name != '\0')
{
name = new char[ len+1 ];
gpa = rhs.gpa;
strCpy( rhs.name, name );
}
return *this;
}
Any my implementation in Main. All im doing here is creating a new student and trying to make it equal to the fourth portion of my student array.
student greg = room[4];
but when i print out the name for greg by just cout << greg.name in a print function, it contains garbage! Any help would be greatly apppreciated, thankyou.
I made my own string copy function with the source being first and the dest being second, so I know thats not the problem. It seems that after using my copy constructor, and I pass the new student to a print function to get printed, the name results in partly garbage. the name is there, but it is followed by garbage. And this same occurence happens when i try to reprint the student I copied from even tho this student name obviously did not contain any garbage before. However if i create a new print funtion where i pass the address of the student instead:
print( &room[4] ) INSTEAD OF print( room[4] )
it prints the name fine! Im just wondering why this is, because i should just be able to pass a student and have it print correctly.
I knew there was a windows function with that exact same name - so that will be an error on my part. I suppose the function to calculate the sting length is also your design?
Can you post the whole code, including your string functions>
int student :: pop( student* &arr, char* filename )
{
int size;
int count;
char* tmp = new char [20];
int len;
ifstream fin;
char* nam;
double gp;
fin.open( filename );
fin >> size;
arr = new student [size+10];
for( count = 0; count<= size; count++ )
{
fin >> tmp;
len = arr->strLen(tmp);
nam = new char[ len+1 ];
arr->strCpy( tmp, nam );
fin >> gp;
arr[count] = student( gp, nam );
}
fin.close();
return size;
}
// the following print function prints using the address of each student and prints the name correctly
void student :: print( student* arr )
{
To start with,
your strCpy function is not doing the copy correctly - it is not terminating the destination correctly (it is not putting the '\0' terminator in the destination string) - fatal error.
EDIT: (I will add these errors as I find them)
The copy constructor is incorrect- The value specified for the Length (name = newchar[ len ]; ) is not enough - fatal error
EDIT:
This one isn't particularly an error - more of a design flaw.
For the print functions, you pass a student object or a pointer_to_student to the function.
This means that a student object doesn't print itself directly.
For example - this means that for a student object to print itself you have to do this:
1 2 3 4
student s;
s.print(&s); //pass a pointer of itself to it self for printing
//or
s.print2(s); //pass a copy of itself to itself for printing
The second method is particulary bad as it means uneccessary copying of the object.
The print functions could be reduced to a single function that takes no parameters