Good Afternoon,
I'm fairly new to C++ programming, right now I'm having difficulties completing an assignment that consist of a student database. I really appreciated if someone would help me or give me some guidelines to complete the assignment. The assignment consists of that the database only generates one text file students.dat. The information of all the students will be stored in this file in records. Each student’s information is saved in one record that occupies one
line in the file. The student’s information includes his/her SSN (9 digits), full name (first last), birth date (mm/dd/yyyy) and the department name that the student belongs to. The program shall repeatedly display a user menu showing the following options:
Enter 1 to add one student (record) to the file at a time
Enter 2 to print all the students (one line for one student)
Enter 3 to search for a student by the name. If the student is found, then print the complete information about the student, otherwise print “No, this student is not in the database.”
Enter 4 to continue
Enter 5 to quit
You are required to design four classes: nameType, dateType, personType and studentType in respective header files
nameType.h, dateType.h, personType.h and studentType.h
Basic sets of data members of those classes are given as follows:
nameType has two string type data members firstName and lastName.
dateType has three integer type data members month, day and year to save respectively the month, the day and the year.
personType includes three data members – SSN in integer type to save the social security number, name in nameType to save the name, and birthDate in dateType to save the birthday.
studentType is derived from personType, but has one new string type data member deptName
In addition, you shall have a header file hwOne_head.h in which you define the following functions
showMenu: to display the user menu.
loadStudents: to read all the students from the database file student.dat and store them in an array studentList of studentType.
insertStudent: to get one student from the user and add it into the array studentList.
searchByName: to read a name (first last) from the user and search the array studentList for the student with the given name
saveStudents: to save all the students in the array studentList to the database student.dat
In the implementation of those functions, you need to use the overloaded stream insertion operator (<<)
So far, I have this done.
`
hwOne.cpp
#include <cstdlib>
#include <iomanip>
#include "nameType.h"
#include "dateType.h"
#include "personType.h"
#include "studentType.h"
#include "hwOne_head.h"
using namespace std;
int main( )
{
showMenu();
//define a const array size
const int SIZE = 35; //class size
const int COLUMN = 11; //column size
//file var
ifstream inFile; //input file
int row, col; //loop var
//open file
inFile.open("students.dat");
if(!inFile)
{
cout<<"Fail to open students.dat, and stop ."<<endl;
return 1;
}
loadStudents(ifstream & inFile, studentType studentList[],
const int SIZE, const int COLUMN);
//close inFile
inFile.close();
void insertStudent(ifstream & inFile, studentType studentList[],
const int SIZE, const int COLUMN);
personType cat;
cin>>cat;
cout<<cat;
return 0;
}
hwOne_head.h
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <fstream>
#include "nameType.h"
#include "dateType.h"
#include "studentType.h"
#include "personType.h"
using namespace std;
#ifndef HW1_HEAD.H
#define HW1_HEAD.H
void loadStudents(ifstream & inFile, studentType studentList[],
const int SIZE, const int COLUMN)
{
char str[1024];
int SSN;
dateType birthday;
nameType name;
string deptName;
double * abc;
abc = new double[COLUMN];
for (int row = 0; row < SIZE; row++)
{
inFile >> SSN
>> name
>> birthday
>> deptName;
for (int col = 0; col <COLUMN; col++)
{
inFile >> abc[col];
}
studentList[row].setStudent(SSN, name, birthday, deptName);
}
}
void insertStudent(ifstream & inFile, studentType studentList[],
const int SIZE, const int COLUMN)
{
int SSN;
dateType birthday;
nameType name;
string deptName;
double * temp;
temp = new double[COLUMN];
for (int row = 0; row < SIZE; row++)
{
inFile.close();
bool found = false;
cout << "Please enter the name of the new student: (firstname and lastname)";
cin >> studentList[row].name;
char dummy;
int month, day, year;
cout << "Please enter the date: (month/day/year)";
cin >> studentList[row].month>>dummy>>studentList[row].day>>dummy>>studentList[row].year;
int id;
cout << "Please enter the SSN:";
cin >> studentList[row].id;
string dp;
cout << "Please enter the department name:";
cin >> studentList[row].deptName;
row++;
}
}
void showMenu()
{
cout <<"**************Welcome to the Student Database Program Menu*************"<<endl;
cout <<"Enter 1 to ADD one student(record) to the file at a time"<<endl;
cout <<"Enter 2 to PRINT all the students"<<endl;
cout <<"Enter 3 to SEARCH for a student by the name"<<endl;
cout <<"Enter 4 to CONTINUE"<<endl;
cout <<"Enter 5 to QUIT"<<endl;
cout <<"***********************************************************************"<<endl;
cout <<endl;
}
#endif
`
nameType.h
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
#ifndef NAME
#define NAME
/*************************************************************
declare a nameType class
************************************************************/
class nameType
{
friend ostream& operator<<(ostream&, const nameType&);
friend istream& operator>>(istream&, nameType&);
friend void printName(const nameType &);
public:
bool operator==(const nameType&) const; // == overloading
bool operator<(const nameType&) const; // > overloading
bool operator>(const nameType&) const; // < overloading
const nameType & operator=(const nameType&);// = overloading
void setName(string,string); // set the full name
void getName(string&, string&); // get the full name
nameType(); // default constructor
nameType(string, string); // a constructor with parameters
nameType(nameType&); // copy constructor
~nameType(); //destructor
private:
dateType.h
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
#ifndef DATE
#define DATE
/*************************************************************
declare a dateType class
************************************************************/
class dateType
{
friend ostream& operator<<(ostream&, const dateType&);
friend istream& operator>>(istream&, dateType&);
public:
bool operator==(const dateType&) const; // == overloading
bool operator<(const dateType&) const; // > overloading
bool operator>(const dateType&) const; // < overloading
const dateType & operator=(const dateType&);// = overloading
void setDate(int, int, int); // set the full date
void getDate(int&, int&, int&); // get the full date
dateType(); // default constructor
dateType(int, int, int); // a constructor with parameters
dateType(dateType&); // copy constructor
~dateType(); //destructor
private:
int month; // store the month
int day; // store the date
int year; // store the year
};
//overload operator<<, which is not a member of dateType, but is a friend to it.
ostream& operator<<(ostream& os, const dateType& adate)
{
os<<adate.month << "/" <<adate.day << "/" <<adate.year;
return os;
}
//overload operator>>, which is not a member of dateType, but is a friend to it.
istream& operator>>(istream& is, dateType& adate)
{
char dummy;
if (is==cin)
{
cout<<"Please enter the date (month/day/year): ";
}
is>>adate.month>>dummy;
is>>adate.day>>dummy;
is>>adate.year;
return is;
}
//overload == operator
bool dateType::operator==(const dateType& adate)const
{
return month == adate.month
&& day == adate.day
&& year == adate.year;
}
//overload < operator
bool dateType::operator<(const dateType& adate) const
{
if (year < adate.year)
return true;
else if (month < adate.month)
return true;
else
return day < adate.day;
}
//overload > operator
bool dateType::operator>(const dateType& adate) const
{
if (year > adate.year)
return true;
else if (month > adate.month)
return true;
else
return day > adate.day;
}
//copy constructor
const dateType & dateType::operator=(const dateType & adate)
{
if (this != & adate)
{
month = adate.month; // set the month
}
return *this;
}
//setdate
void dateType::setDate(int mon, int d, int yr)
{
month = mon; // set the month
day = d; // set the date
year = yr; // set the year
}
//getdate
void dateType::getDate(int & mon, int & d, int & yr)
{
mon = month; // get the month
d = day; // get the day
yr = year; // get the year
}
//default constructor
dateType::dateType()
{
month = day = year = -1; // set the month to null
}
//another constructor
dateType::dateType(int mon, int d, int yr) // a constructor
{
month = mon; // set the month
day = d; // set the date
year = yr; // set the year
}
//copy constructor
dateType::dateType(dateType & adate)
{
if (this != & adate)
{
month = adate.month; // set the month
day = adate.day; // set the date
year = adate.year; // set the year
}
}
// desconstructor
dateType::~dateType( )
{
cout<<" Oh, I am dying ..."<<endl;
}
#endif
string firstName; // store the first name
string lastName; // store the last name
};
//overload operator<<, which is not a member of nameType, but is a friend to it.
ostream& operator<<(ostream& os, const nameType& aName)
{
os<<aName.firstName + " " + aName.lastName <<endl;
return os;
}
//overload operator>>, which is not a member of nameType, but is a friend to it.
istream& operator>>(istream& is, nameType& aName)
{
cout<<"Please enter the name (first last): ";
is>>aName.firstName;
is>>aName.lastName;
return is;
}
//define printName, which is not a member of nameType, but is a friend to it.
void printName(const nameType & aName)
{
cout<<aName;
}
//overload == operator
bool nameType::operator==(const nameType& aName)const
{
return firstName == aName.firstName
&& lastName == aName.lastName;
}
//overload < operator
bool nameType::operator<(const nameType& aName) const
{
if (lastName < aName.lastName)
return true;
else
return lastName == aName.lastName
&& firstName < aName.firstName;
}
//overload > operator
bool nameType::operator>(const nameType& aName) const
{
if (lastName > aName.lastName)
return true;
else
return lastName== aName.lastName
&& firstName > aName.firstName;
}
//copy constructor
const nameType & nameType::operator=(const nameType & aName)
{
if (this != & aName)
{
firstName = aName.firstName; // set the first name
lastName = aName.lastName; // set the last name
}
return *this;
}
//setName
void nameType::setName(string first, string last)
{
firstName = first; // set the first name
lastName = last; // set the last name
}
//getName
void nameType::getName(string & first, string & last)
{
first = firstName; // get the first name
last = lastName; // get the last name
}
//default constructor
nameType::nameType() // default constructor
{
firstName = "Cat"; // set the first name to null string
lastName = "Dog"; // set the second name to null string
}
//another constructor
nameType::nameType(string first, string last) // a constructor
{
firstName = first; // set the first name
lastName = last; // set the last name
}
//copy constructor
nameType::nameType(nameType & aName)
{
if (this != & aName)
{
firstName = aName.firstName; // set the first name
lastName = aName.lastName; // set the last name
}
}
// desconstructor
nameType::~nameType( )
{
cout<<" Oh, I am dying ..."<<endl;
}
#endif
personType.h
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include "nameType.h"
#include "dateType.h"
using namespace std;
#ifndef PERSON
#define PERSON
/*************************************************************
declare a personType class
************************************************************/
class personType
{
friend ostream& operator<<(ostream&, const personType&);
friend istream& operator>>(istream&, personType&);
public:
bool operator==(const personType&) const; // == overloading
bool operator<(const personType&) const; // > overloading
bool operator>(const personType&) const; // < overloading
const personType & operator=(const personType&);// = overloading
nameType getName(){return name;}
dateType getDate(){return birthday;}
void setPerson(int, nameType, dateType); // set the full person
void getPerson(int&, nameType&, dateType&); // get the full person
personType(); // default constructor
personType(int, nameType, dateType); // a constructor with parameters
personType(personType&); // copy constructor
~personType(); //destructor
private:
int SSN; // store the SSN
nameType name; // store the name
dateType birthday; // store the birthday
};
//overload operator<<, which is not a member of personType, but is a friend to it.
ostream& operator<<(ostream& os, const personType& aperson)
{
os<<aperson.SSN <<" " <<aperson.name <<aperson.birthday<<endl;
return os;
}
//overload operator>>, which is not a member of personType, but is a friend to it.
istream& operator>>(istream& is, personType& aperson)
{
nameType n;
dateType d;
int id;
if (is==cin)
{
cout<<"Please enter SSN:";
}
is>>id>>n>>d;
aperson.setPerson(id, n, d);
return is;
}
//overload == operator
bool personType::operator==(const personType& aperson)const
{
return SSN == aperson.SSN
&& name == aperson.name;
}
//overload < operator
bool personType::operator<(const personType& aperson) const
{
return birthday < aperson.birthday;
}
//overload > operator
bool personType::operator>(const personType& aperson) const
{
return birthday > aperson.birthday;
}
//copy constructor
const personType & personType::operator=(const personType & aperson)
{
if (this != & aperson)
{
name = aperson.name;
birthday = aperson.birthday;
SSN = aperson.SSN;
}
return *this;
}
//setperson
void personType::setPerson(int id, nameType n, dateType d)
{
SSN = id;
name = n;
birthday = d;
}
//getperson
void personType::getPerson(int & id, nameType & n, dateType & d)
{
id = SSN;
n = name;
d = birthday;
}
//default constructor
personType::personType() // default constructor
{
SSN = -1111;
}
//another constructor
personType::personType(int id, nameType n, dateType d) // a constructor
{
SSN = id;
name = n;
birthday = d;
}
//copy constructor
personType::personType(personType & aperson)
{
if (this != & aperson)
{
name = aperson.name;
birthday = aperson.birthday;
SSN = aperson.SSN;
}
}
// desconstructor
personType::~personType( )
{
cout<<" Oh, I am dying ..."<<endl;
}
#endif
studentType.h
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include "dateType.h"
#include "nameType.h"
#include "personType.h"
using namespace std;
#ifndef STUDENT
#define STUDENT
/*************************************************************
declare a studentType class
************************************************************/
class studentType:public personType
{
friend ostream& operator<<(ostream&, const studentType&);
friend istream& operator>>(istream&, studentType&);
public:
bool operator==(const studentType&) const; // == overloading
bool operator<(const studentType&) const; // > overloading
bool operator>(const studentType&) const; // < overloading
const studentType & operator=(const studentType&);// = overloading
void setStudent(int, nameType&, dateType, string); // set the full student
void getStudent(int&, nameType&, dateType& , string& ); // get the full student
studentType(); // default constructor
studentType(string, string); // a constructor with parameters
studentType(studentType&); // copy constructor
~studentType(); //destructor
private:
string deptName; // store the department name
};
//overload operator<<, which is not a member of studentType, but is a friend to it.
ostream& operator<<(ostream& os, const studentType& astudent)
{
os<<astudent.getSSN()
<<" " <<astudent.getName() <<" "
<<astudent.getDate() <<" " <<astudent.deptName <<endl;
return os;
}
//overload operator>>, which is not a member of studentType, but is a friend to it.
istream& operator>>(istream& is, studentType& astudent)
{
int id;
nameType n;
dateType d;
if(is==cin)
{
cout<<"Please enter the SSN: ";
}
is>>id;
is>>n>>d;
if(is==cin)
{
cout << "Enter the department:";
}
astudent.deptName;
astudent.setPerson(id, n, d);
return is;
}
//overload == operator
bool studentType::operator==(const studentType& astudent)const
{
return getSSN() == astudent.getSSN()
&& getName() == astudent.getName();
}
//overload < operator
bool studentType::operator<(const studentType& astudent) const
{
return getDate() < astudent.getDate();
}
//overload > operator
bool studentType::operator>(const studentType& astudent) const
{
return getDate() > astudent.getDate();
}
//copy constructor
const studentType & studentType::operator=(const studentType & astudent)
{
nameType n;
dateType d;
int id;
if (this != & astudent)
{
astudent.getPerson(id, n, d);
setPerson(id, n, d);
deptName = astudent.deptName;
}
return *this;
}
//setstudent
void studentType::setStudent(int id, nameType &n, dateType d, string dp)
{
setPerson(id, n, d);
deptName = dp;
}
//getstudent
void studentType::getStudent(int& id, nameType& n, dateType& d, string& dp)
{
getPerson (id, n, d);
dp = deptName;
}
//default constructor
studentType::studentType() // default constructor
{
deptName = "CS";
}
//copy constructor
studentType::studentType(studentType & astudent)
{
nameType n;
dateType d;
int id;
if (this != & astudent)
{
astudent.getPerson(id, n, d);
setPerson(id, n, d);
deptName = astudent.deptName;
}
return *this
}
// desconstructor
studentType::~studentType( )
{
cout<<" Oh, I am dying ..."<<endl;
}
#endif