Hi, I have just finished this large program that reads information about a person and prints the results to a txt file for output. I was wondering if there is anything I can do to make it look better, ie: more comments, better use of memory allcoation ect. The code does compile and I do get the desired output I wanted. Please be cruel One thing i should of done is placed the classes into sepeate files to increase program readability.
Code:
// student database
// by peter watts
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::cerr;
using std::fixed;
#include <string>
using std::string;
using std::getline;
#include <iomanip>
using std::setprecision;
using std::setw;
#include <fstream>
using std::ofstream;
using std::ifstream;
// base class holds shared data for all classes
class Base
{
public:
Base();
~Base();
protected:
string schoolName;
unsigned short studentAmount;
};
// set member values to zero
Base::Base()
{
string tempSchoolName = "";
schoolName = tempSchoolName;
unsigned short tempStudentAmount = 0;
studentAmount = tempStudentAmount;
}
// destructor does nothing
Base::~Base()
{
cout << "\nBase class destructor called...\n";
}
// student class holds data regarding individual student
class Student : public Base
{
public:
Student();
~Student();
string getStudentName() const;
string getAddressLine1() const;
string getAddressLine2() const;
string getAddressLine3() const;
string getPostcode() const;
unsigned short getAge() const;
// shared data acsessor functions
string getSchoolName() const;
unsigned short getStudentAmount() const;
void setStudentName ( string );
void setAddressLine1 ( string );
void setAddressLine2 ( string );
void setAddressLine3 ( string );
void setPostcode ( string );
void setAge ( unsigned short );
// shared data acsessor functions
void setSchoolName ( string );
void setStudentAmount ( unsigned short );
void printReport ( string, string, string, string, string, unsigned short, string, unsigned short );
private:
string studentName;
string addressLine1;
string addressLine2;
string addressLine3;
string postcode;
unsigned short *age;
};
// assign all data members to zero
Student::Student()
{
age = new unsigned short(0); // memory on heap space
string tempStudentName = "";
studentName = tempStudentName;
string tempAddressLine1 = "";
addressLine1 = tempAddressLine1;
string tempAddressLine2 = "";
addressLine2 = tempAddressLine2;
string tempAddressLine3 = "";
addressLine3 = tempAddressLine3;
string tempPostcode = "";
postcode = tempPostcode;
}
// destroy age pointer
Student::~Student()
{
delete age;
age = NULL;
}
////////////////////// student class return functions //////////////////////////
string Student::getStudentName() const
{
return studentName;
}
string Student::getAddressLine1() const
{
return addressLine1;
}
string Student::getAddressLine2() const
{
return addressLine2;
}
string Student::getAddressLine3() const
{
return addressLine3;
}
string Student::getPostcode() const
{
return postcode;
}
unsigned short Student::getAge() const
{
return *age;
}
string Student::getSchoolName() const
{
return schoolName;
}
unsigned short Student::getStudentAmount() const
{
return studentAmount;
}
///////////////////////// student class setter functions ///////////////////////
void Student::setStudentName ( string tempStudentName )
{
cout << "\nEnter student full name: ";
getline ( cin, tempStudentName );
studentName = tempStudentName;
setAddressLine1("");
return;
}
void Student::setAddressLine1 ( string tempAddressLine1 )
{
cout << "\nEnter first address line: ";
getline ( cin, tempAddressLine1 );
addressLine1 = tempAddressLine1;
setAddressLine2("");
return;
}
void Student::setAddressLine2 ( string tempAddressLine2 )
{
cout << "\nEnter second address line: ";
getline ( cin, tempAddressLine2 );
addressLine2 = tempAddressLine2;
setAddressLine3("");
return;
}
void Student::setAddressLine3 ( string tempAddressLine3 )
{
cout << "\nEnter third address line: ";
getline ( cin, tempAddressLine3 );
addressLine3 = tempAddressLine3;
setPostcode("");
return;
}
void Student::setPostcode ( string tempPostcode )
{
cout << "\nEnter postcode: ";
getline ( cin, tempPostcode );
postcode = tempPostcode;
setAge(0);
return;
}
void Student::setAge ( unsigned short m_age )
{
cout << "\nEnter student age: ";
cin >> m_age;
*age = m_age;
cin.ignore();
setSchoolName("");
return;
}
void Student::setSchoolName ( string tempSchoolName )
{
cout << "\nEnter name of college: ";
getline ( cin, tempSchoolName );
schoolName = tempSchoolName;
setStudentAmount(0);
return;
}
void Student::setStudentAmount ( unsigned short tempStudentAmount )
{
cout << "\nEnter total student amount: ";
cin >> tempStudentAmount;
studentAmount = tempStudentAmount;
printReport("","","","","",0,"",0);
return;
}
// on the cboard editor, the printFunction parameters are missing some details, but they are
// all included in the orgigial program
void Student::printReport ( string tempStudentName, string tempAddressLine1,
string tempAddressLine2, string tempAdressLine3,
string tempPostcode, unsigned short m_age,
string tempSchoolName, unsigned short tempStudentAmount )
{
ofstream OUT_FILE ("Student.txt");
OUT_FILE<<getStudentName() << endl
<<getAddressLine1() << endl
<<getAddressLine2() << endl
<<getAddressLine3() << endl
<<getPostcode() << endl
<<getAge() << endl
<<getSchoolName() << endl
<<getStudentAmount() << endl;
return;
}
// main - driver ///////////////////////////////////////////////////////////////
int main ( void )
{
int choice = 0;
Student *stu = new Student;
// loop to detect correct input
while (( choice >= 0 ) || ( choice <= 3 ))
{
cout << "\n\tSTUDENT DATABASE\n\n"
<< "1 Add a student to the database\n"
<< "2 Print final report\n"
<< "3 Quit\n"
<< "> ";
cin >> choice;
switch ( choice )
{
case 1:
cin.ignore(); // ignore previous input
stu->setStudentName("");
break;
case 2:
break;
case 3:
return 0;
break;
// don't really use this
default:
break;
}
}
// ensure heap pointer is properly
// deleted on program termination
delete stu;
stu = NULL;
return 0;
}