swap function incorrect output
Thanks for all your help!
I'm almost done with the project, but now the swap function is giving me problems. Somehow the output from it is incorrect. Can you take a look at the source and tell me what i'm doing wrong (maybe not just on the swap function. it's my first time writing something like this so i must have made more mistakes)
(the swap function appears to truncate one of the char* values it's swapping)
Thanks!
I'm almost done with the project, but now the swap function is giving me problems. Somehow the output from it is incorrect. Can you take a look at the source and tell me what i'm doing wrong (maybe not just on the swap function. it's my first time writing something like this so i must have made more mistakes)
(the swap function appears to truncate one of the char* values it's swapping)
Thanks!
#include <iostream>
using namespace std;
class Student
{
private:
char *fname;
char *lname;
unsigned int age;
public:
Student(char *f, char *l, unsigned const int _age); // constructor
~Student(); // destructor
Student(const Student &s); // copy constructor;
void set_fname(const char * str);
void set_lname(const char * str);
void set_age(const unsigned int num) { age = num; }
const char * get_fname() { return fname ; }
const char * get_lname() { return lname ; }
const int get_age() { return age ; }
};
Student::Student(char *f = "", char *l = "", unsigned const int _age = 0)
{
fname = new char[strlen(f) + 1];
strcpy(fname, f);
lname = new char[strlen(l) + 1];
strcpy(lname, l);
age = _age;
}
Student::~Student(){
delete[] fname;
delete[] lname;
}
Student::Student(const Student &s) {
fname = new char[strlen(s.fname) + 1];
strcpy(fname, s.fname);
lname = new char[strlen(s.lname) + 1];
strcpy(lname, s.lname);
age = s.age;
}
void Student::set_fname(const char * str) {
delete[] fname;
fname = new char[strlen(str) + 1];
strcpy(fname, str);
}
void Student::set_lname(const char * str) {
delete[] lname;
lname = new char[strlen(str) + 1];
strcpy(lname, str);
}
// PROTOTYPES
void printClass(Student *_class, const unsigned int SIZE);
void swapStudents(Student *st1, Student *st2);
int main() {
unsigned int numStudents;
char strBuffer[100];
int intBuffer;
Student *Class;
cout << "Welcome to the Virtual Classroom"
<< "\n\nEnter the number of students in the class: ";
cin >> numStudents;
Class = new Student[numStudents];
for(int i = 0; i < numStudents; i++) {
cout << "Student #" << i+1
<< "\nFirst Name: ";
cin >> strBuffer;
Class[i].set_fname(strBuffer);
cout << "Last Name: ";
cin >> strBuffer;
Class[i].set_lname(strBuffer);
cout << "Age: ";
cin >> intBuffer;
Class[i].set_age(intBuffer);
cout << endl;
}
printClass(Class, numStudents);
swapStudents(&Class[0], &Class[2]);
printClass(Class, numStudents);
delete[] Class;
return 0;
}
void printClass(Student *_class, const unsigned int SIZE) {
cout << "Printing student names and ages: \n";
for(int i=0; i < SIZE; i++) {
cout << "#" << i+1 << ": " << _class[i].get_lname()
<< ", " << _class[i].get_fname()
<< " - " << _class[i].get_age()
<< endl;
}
}
void swapStudents(Student *st1, Student *st2) {
Student *hold = new Student(*st1);
*st1 = *st2;
*st2 = *hold;
delete hold;
}
