Testing a class using for loop in int main - c++

In my main I have to test the class i made with a for loop but am a little stuck. I have to loop through 3 times and get user input for the ticketing system. I tried making an array and storing the user input but nothing I have tried so far has worked. Im trying to call upon the SetWorkTicket with my loop.
#include<iostream>
#include<iomanip>
#include<stdexcept>
#include<sstream>
using namespace std;
// start of the WorkTicket class
class WorkTicket{
public:
// defailt constructors, if the parameters are not specified the ticket will be set to 0 and the date to 1/1/2000. The rest empty strings
WorkTicket() : myTicketNumber(0), myClientId(""), myDay (1), myMonth (1), myYear(2000), myDescription ("") { }
WorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description);
// mutator method that sets all attributes of the object to the parameters as long as valid. If no problems are being detected set TRUE if not then FALSE.....
bool SetWorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description);
// accessor method that will show all the attributes of the ticket in the console menu
void ShowWorkTicket() const;
// here are the sets and gets for the attributes
// ticket number
void SetTicketNumber(int ticketNumber);
int GetTicketNumber() const {return myTicketNumber;}
// client id
void SetClientId(string clientId) {myClientId = clientId;}
string GetClientId() const { return myClientId;}
// date by day
void SetDay(int day);
int GetDay() const { return myDay; }
// date by month
void SetMonth(int month);
int GetMonth() const { return myMonth; }
// date by year
void SetYear(int year);
int GetYear() const { return myYear; }
// description
void SetDescription(string description) { myDescription = description; }
string GetDescription() const { return myDescription; }
private:
int myTicketNumber;
string myClientId;
int myDay;
int myMonth;
int myYear;
string myDescription;
};
// the work ticket constructor definition
WorkTicket::WorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description)
{
SetTicketNumber(ticketNumber);
SetClientId(clientId);
SetMonth(month);
SetDay(day);
SetYear(year);
SetDescription(description);
}
// set work ticket
bool WorkTicket::SetWorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description)
{
const int MAX_DAY = 31; // setting max day to 31
const int MAX_MONTH = 12; // max month to 12
const int MIN_YEAR = 2000; // the min year 2000 as specified
const int MAX_YEAR = 2099; // max year 2099 as speciified
bool valid = true;
// setting the limits
if(ticketNumber < 0 || month < 1 || month > MAX_MONTH ||
day < 1 || day > MAX_DAY ||
year < MIN_YEAR || year > MAX_YEAR)
valid = false;
else if (clientId.length() < 1 || description.length() < 1)
valid = false;
else
{
myTicketNumber = ticketNumber;
myClientId = clientId;
myDay = day;
myMonth = month;
myYear = year;
myDescription = description;
}
return valid;
}
// this is to show the work ticket, it will show everything in the console..
void WorkTicket::ShowWorkTicket() const
{
cout << "\nWork Ticket Number: " << myTicketNumber
<< "\nClient ID: " << myClientId
<< "\nDate: " << setw(2) << setfill('0') << myMonth
<< "/" << setw(2) << setfill('0') << myDay
<< "/" << myYear
<< "\nIssue: " << myDescription << endl;
}
void WorkTicket::SetTicketNumber(int ticketNumber)
{
if(ticketNumber > 0)
{
myTicketNumber = ticketNumber;
}
else
{
throw invalid_argument("Try Again.");
}
}
// WorkTicket::SetDay definition
void WorkTicket::SetDay(int day)
{
const int MIN_VALID = 1;
const int MAX_VALID = 31;
if(day >= MIN_VALID && day <= MAX_VALID)
{
myDay = day;
}
else
{
throw invalid_argument("Try Again.");
}
}
// WorkTicket::SetMonth definition
void WorkTicket::SetMonth(int month)
{
const int MIN_VALID = 1;
const int MAX_VALID = 12;
if(month >= MIN_VALID && month <= MAX_VALID)
{
myMonth = month;
}
else
{
throw invalid_argument("Try Again.");
}
}
// WorkTicket::SetYear definition
void WorkTicket::SetYear(int year)
{
const int MIN_VALID = 2000;
const int MAX_VALID = 2099;
if(year >= MIN_VALID && year <= MAX_VALID)
{
myYear = year;
}
else
{
throw invalid_argument("Try Again.");
}
}
int main()
{
const int NUMBER_OF_TICKETS = 3;
WorkTicket tickets[NUMBER_OF_TICKETS];
int ticketNumber;
string clientId;
int day;
int month;
int year;
string description;
for (int i = 0; i <NUMBER_OF_TICKETS; i++)
{
cout << "\WorkTicket [" << i << "]: " << endl;
cout << "Enter the ticket number: " << endl;
cout << "Enter the client ID: " << endl;
getline(cin, clientId);
day = SetDay("Enter a day number: ");
}
}

Gather the input and then construct a WorkTicket. Push the tickets into a vector. In the code yet to be written, the newly create WorkTickets can be tested for correctness.
Make sure to include vector at the top of the file.
int main()
{
const int NUMBER_OF_TICKETS = 3;
int ticketNumber;
string clientId;
int day;
int month;
int year;
string description;
std::vector<WorkTicket> tickets;
for (int i = 0; i <NUMBER_OF_TICKETS; i++)
{
cout << "WorkTicket [" << i << "]: " << endl;
cout << "Enter the ticket number: " << endl;
cin>> ticketNumber;
cout << "Enter the client ID: " << endl;
cin >> clientId;
cout << "Enter a day number: " << endl;
cin >> day;
cout << "Enter a month number: " << endl;
cin>> month;
cout << "Enter a year number: " << endl;
cin>> year;
cout << "Enter a description : " << endl;
cin >> description;
try {
tickets.push_back({ticketNumber, clientId, month, day, year, description});
}
catch(std::exception e)
{
std::cout << e.what() << std::endl;
}
}
}

Assuming the rest of your code works correctly and you only have problems in the main:
There are a few issues as commented by others in your code in main:
1) You declared WorkTicket tickets[NUMBER_OF_TICKETS]; but have not used it anywhere.
2) The variable tickets is an array and therefore in the for loop tickets[i] is of type WorkTicket.
3) SetDay is a member function of WorkTicket and takes int as argument.
In light of these, following changes made:
for (int i = 0; i <NUMBER_OF_TICKETS; i++)
{
cout << "\WorkTicket [" << i << "]: " << endl;
cout << "Enter the ticket number: " << endl;
cout << "Enter the client ID: " << endl;
getline(cin, clientId);
cout<<"Enter a day number:";
int day;
cin>>day;
tickets[i].SetDay(day); //day is int and tickets[i] is of WorkTicket
}
Hope this helps to understand the main part better.

Related

FOR LOOP fails to iterate through the object vector where purchase transactions are stored

I'm unsure why when more than one transaction has been made the FOR LOOP won't iterate through the vector of "buyers". Am I missing something simple? I've spent the last few hours trying to fix this but to no avail. If any one could possibly help with my problem I'd be eternally grateful.
#include <iostream>
#include <iomanip>
#include <vector>
#include <cctype>
using namespace std;
//This class is for the car details that the user can purchase.
class cCar {
private:
string _sName;
double _dPrice;
public:
cCar(string s, double d)
{
_sName = s;
_dPrice = d;
}
string getName() { return _sName; }
double getPrice() { return _dPrice; }
};
//This is where all the car detail purchases are created and stored in the object 'carlist'
vector<cCar> CarDatabase(vector<cCar>& car_list)
{
car_list.push_back(cCar("Blue Nissan Skyline", 1000));
car_list.push_back(cCar("Red Mini", 3000));
car_list.push_back(cCar("Black Land Rover", 4000));
car_list.push_back(cCar("Beatle", 9000));
car_list.push_back(cCar("Ferrari", 300000));
return car_list;
}
//This class stores the user's transactions
class Finance {
private:
string _sUserName;
double _dCostOfCar;
string _sChosenCar;
int _iFinancePlan;
double _dDepositedAmount;
double _dMonthlyPayments;
double _dTotalLeftToPay;
public:
Finance(string sName, double dCostOfCar, string sChosenCar, int iFinancePlan, double dDepositedAmount, double dDMonthlyPayments, double dTotalLeftToPay)
{
_sUserName = sName;
_dCostOfCar = dCostOfCar;
_sChosenCar = sChosenCar;
_iFinancePlan = iFinancePlan;
_dDepositedAmount = dDepositedAmount;
_dMonthlyPayments = dDMonthlyPayments;
_dTotalLeftToPay = dTotalLeftToPay;
}
string getUserName() { return _sUserName; }
double getdCostOfCar() { return _dCostOfCar; }
string getChosenCar() { return _sChosenCar; }
int getFinancePlan() { return _iFinancePlan; }
double getDepositAmount() { return _dDepositedAmount; }
double getMonthlyAmount() { return _dMonthlyPayments; }
double getTotalLeftToPay() { return _dTotalLeftToPay; }
};
//1. This displays the car menu items.
void display_menu(vector<cCar>& car_list)
{
cout << "\nMENU";
for (int iCount = 0; iCount != car_list.size(); iCount++) {
cout << "\n" << iCount + 1 << ". " << car_list[iCount].getName();
cout << "\n\tPrice: £" << car_list[iCount].getPrice();
cout << "\n";
}
}
//This procedure proccesses the user's selection and all information regarding price and name of car are then transferred to transaction variables.
void selectedCar(vector<cCar>& car_list, string& sNameOfChosenCar, double& dCostOfChosenCar)
{
int iSelectionFromMenu = -1;
do {
cout << "\nChoose a car that you'd wish to buy from the menu (1 - " << car_list.size() << "): ";
cin >> iSelectionFromMenu;
if (iSelectionFromMenu > 0 && iSelectionFromMenu <= car_list.size()) {
sNameOfChosenCar = car_list[iSelectionFromMenu - 1].getName();
dCostOfChosenCar = car_list[iSelectionFromMenu - 1].getPrice();
}
else {
cout << "\nPlease enter valid number!";
iSelectionFromMenu = -1;
}
} while (iSelectionFromMenu == -1);
}
//This procedure gets from the user their preferred finance plan through their input.
void FinanceLength(int& iFinanceLength)
{
do {
cout << "\nHow long do you wish for your finance plan to last? (1 - 4 years): ";
cin >> iFinanceLength;
if (iFinanceLength < 0 || iFinanceLength > 4) {
cout << "\nOops, try again! Please enter between 1 - 4!";
}
} while (iFinanceLength < 0 || iFinanceLength > 4);
}
//This procedure gets the user's deposit.
void DepositMoney(double& dDepositAmount)
{
do {
cout << "\nEnter deposit amount (minimum £500 accepted): £";
cin >> dDepositAmount;
if (dDepositAmount < 500) {
cout << "\nTry again! Deposit an amount greater than or equal to £500.";
}
} while (dDepositAmount < 500);
}
//This function calculates the amount of money the user has to pay after deposit, added tax and charge percentage of 10%
double TotalLeftToPay(double iFinanceLength, double dDepositAmount, double dCostOfChosenCar)
{
double dChargePercentage = 0.10;
double dTotalLeftToPay = dCostOfChosenCar + (dCostOfChosenCar * dChargePercentage) - dDepositAmount + 135;
return dTotalLeftToPay;
}
//This calculates monthly payments.
double MonthlyPayments(double dTotalLeftToPay, int iFinanceLength)
{
double dMonthlyPayments = (dTotalLeftToPay / iFinanceLength) / 12;
return dMonthlyPayments;
}
//This asks the user whether they'd like to restart the application.
void RestartOptions(char& cOption, bool& bExit)
{
do {
cout << "\nDo you wish to make another purchase? (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
switch (cOption) {
case 'Y':
bExit = false;
break;
case 'N':
bExit = true;
break;
default:
cout << "Sorry, that's an invalid input, please try again!";
continue;
}
} while (cOption != 'y' && cOption != 'Y' && cOption != 'n' && cOption != 'N');
}
//This string function returns either year or years (plural)
string YearOrYears(int iFinanceLength)
{
return (iFinanceLength > 1) ? "years" : "year";
}
//This displays receipt of the user's transaction.
//HERE IS WHRERE I "M STRUGGLING TO ITERATE
void Receipt(const string& sUserName, const int& iFinanceLength, const double& dDepositAmount, char cOption, bool& bExit, const string& sNameOfChosenCar, const double& dCostOfChosenCar, vector<Finance>& buyers)
{
double dTotalLeftToPay = TotalLeftToPay(iFinanceLength, dDepositAmount, dCostOfChosenCar);
double dMonthlyPayments = MonthlyPayments(dTotalLeftToPay, iFinanceLength);
buyers.push_back(Finance(sUserName, dCostOfChosenCar, sNameOfChosenCar, iFinanceLength, dDepositAmount, dMonthlyPayments, dTotalLeftToPay));
for (int iCount = 0; iCount != buyers.size(); iCount++) {
cout << "\nReceipt for: " << buyers[iCount].getUserName() << ". ";
cout << "\nYou have chosen " << buyers[iCount].getChosenCar() << ".";
cout << "\nYour finance plan timescale is " << buyers[iCount].getFinancePlan() << " " << YearOrYears(iFinanceLength) << ".";
cout << "\nYou've deposited £" << buyers[iCount].getDepositAmount() << ".";
cout << "\nTotal left to pay: £" << buyers[iCount].getTotalLeftToPay();
cout << "\nMonthly Payments: £" << buyers[iCount].getMonthlyAmount();
cout << "\n";
}
RestartOptions(cOption, bExit);
}
//This asks the user whether they're happy with the options of they've chosen.
void AcceptDeclineOptions(string& sUserName, int& iFinanceLength, double& dDepositAmount, bool& bExit, string& sNameOfChosenCar, double& dCostOfChosenCar, vector<Finance> buyers)
{
char cOption = 0;
do {
cout << "\nConfirm finance plan (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
if (cOption == 'Y' || cOption == 'N') {
if (cOption == 'Y') {
Receipt(sUserName, iFinanceLength, dDepositAmount, cOption, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
}
else {
RestartOptions(cOption, bExit);
}
}
else {
cout << "\nSorry, that's not a valid command.";
}
} while (cOption != 'Y' && cOption != 'N');
}
int main()
{
bool bExit = false;
int iFinanceLength = 0;
double dDepositAmount = 0;
string sNameOfChosenCar = "";
double dCostOfChosenCar = 0;
vector<cCar> car_list;
CarDatabase(car_list);
vector<cCar> car_purchases;
vector<Finance> buyers;
do {
cout << "Welcome!";
string sUserName = "";
cout << "\nEnter your name: ";
cin >> sUserName;
display_menu(car_list);
selectedCar(car_list, sNameOfChosenCar, dCostOfChosenCar);
FinanceLength(iFinanceLength);
DepositMoney(dDepositAmount);
AcceptDeclineOptions(sUserName, iFinanceLength, dDepositAmount, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
} while (bExit == false);
}

What should I change or delete in order to fix this code so the rest may work fine?

#include <h1>iostream <h2>
#include <h1>cmath<h2>// for + of months, days, and years
#include <h1>fstream<h2> //for in and output
#include <h1>cstdlib<h2>
#include <h1>string<h2>//for string type
using namespace std;
class Device {//Input and store Device Description and Serial Numbers
protected:
string serial_number;
string device_description;
public:
Device() {
serial_number = ("6DCMQ32");
device_description = ("TheDell");
}
Device(string s, string d) {
serial_number = s;
device_description = d;
}
};
class Test {
protected:
string Test_Description;
static int recent_month, recent_day, recent_year, new_month;
static int nmonth, next_month, next_day, next_year, max_day;
public:
static void getMonth() {//Calculates the next/new month
next_month = recent_month + nmonth;
new_month = next_month % 12;
if (next_month >= 12) {
cout << "The next Date: " << new_month << " / ";
}
else {
cout << "The next Date: " << next_month << " / ";
}
}
static void getDay() { //Calculates day of next month
if (new_month == 4 || new_month == 6 || new_month == 9 || new_month == 11) {
max_day = 30;
}
else if (new_month == 2) {
max_day = 29;
}
else {
max_day = 31;
}
if (recent_day > max_day) {
cout << max_day << " / ";
}
else {
cout << recent_day << " / ";
}
}
static void getYear() {//Calculates the year of the next number of months
next_year = recent_year + next_month;
if (next_year >= 12) {
cout << recent_year + (next_month / 12) << endl;
}
else {
cout << next_year << endl;
}
}
static void getDate() {
Test::getMonth(), Test::getDay(), Test::getYear();
}
};
int Test::recent_month, Test::recent_day, Test::recent_year,
Test::new_month;
int Test::nmonth, Test::next_month, Test::next_day, Test::next_year,
Test::max_day;
class Lab : public Device, public Test {//Class Lab is a Child of Class Test and Class Device
protected:
static int n;
public:
friend istream & operator>>(istream & cin, const Lab & lab) {
cout << "Enter Device Description and serial number: ";
getline(cin, lab.device_description);//This is where the error is
getline(cin, lab.serial_number);//This is where the error is
cout << "Enter Test Description: ";
getline(cin, lab.Test_Description);//This is where the error is
cout << "Enter number of months: ";
cin >> lab.nmonth;
cout << "Enter the most recent date(mm/dd/yyyy): ";
cin >> lab.recent_month >> lab.recent_day >> lab.recent_year;
return cin;
}
friend ostream & operator<<(ostream & cout, const Lab & lab) {
cout << lab.device_description << " ";
cout << lab.serial_number << endl;
cout << lab.Test_Description << endl;
getDate();
return cout;
}
static void getFile() {
cout << "Enter the number of devices: ";
cin >> n;
Lab *obj = new Lab[n];
if (obj == 0) {
cout << "Memory Error";
exit(1);
}
for (int i = 0; i<n; i++) {
cin >> obj[i];
}
ofstream myfile("Device.dat", ios::binary);
myfile.write((char *)obj, n * sizeof(Lab));
Lab *obj2 = new Lab[n];
ifstream file2("Device.dat", ios::binary);
if (obj2 == 0) {
cout << "Memory Error";
exit(1);
}
file2.read((char *)obj2, n * sizeof(Lab));
for (int i = 0; i < n; i++) {
cout << obj2[i];
cout << endl;
}
delete[] obj2;
}
void getSearch(){
}
};
void main() {
Lab L;
L.getFile();
system("pause");
}
//Error C2665 'std::getline': none of the 2 overloads could convert all
the argument types
/*
Purpose: is to enter the number of months for the next test date of device with input of serial number, Device Description, Test Description, recent date, and the number of months of two tests. At the end the program must be searched by having the user to input the serial number and the next date, if these two are valid everything in the device is listed out.
*/<
You shouldn't name your parameters like objects of the standard library (cin).
For an argument to be modifiable it must not be a reference to a constant entity.
Also, a std::istream bound operator<<() overload should not do output, but only extract the object required from the stream.

I don't understand why this code isn't performing properly

/* Program Name: BadDate.cpp
Function: This program determines if a date entered by the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that an invalid date was entered
*/
#include <iostream>
bool validateDate(int, int, int);
using namespace std;
int main()
{
// Declare variables
int year;
int month;
int day;
const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
bool validDate = true;
This is the work of the housekeeping() method
Get the year, then the month, then the day
int housekeeping() {
cout << "Please enter a year: "<< endl;
cin >> year;
cout << "Please enter a month: "<< endl;
cin >> month;
cout << "Please enter a day: "<< endl;
cin >> day;
}
This is the work of the detailLoop() method
Check to be sure date is valid
int detailLoop() {
if(year <= MIN_YEAR) // invalid year
validDate = false;
else if (month < MIN_MONTH || month > MAX_MONTH) // invalid month
validDate = false;
else if (day < MIN_DAY || day > MAX_DAY) // invalid day
validDate = false;
}
This is the work of the endOfJob() method
test to see if date is valid and output date and whether it is valid or not
int endOfJob()
{
if(validDate == true)
{
// Output statement
cout << " Your date is valid!"<< endl;
}
else
{
// Output statement
cout << " Your date is not valid!"<< endl;
}
}
} // end of main() function

Debug error, abort() has been called in c++

I'm trying to perform error checking on my initializer value for date, so that a day or month outside the acceptable range will stop the program from debbuging, but I'm getting a debug error even for the acceptable range. I don't know where the error is from, so i posted the code.
// ConsoleApplication56.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdexcept>
#include<array>
using namespace std;
class Date
{
public:
explicit Date(int mo, int da, int ye)
{
setDate( mo, da, ye);
}
void setDate(int &m, int &d, int &y)
{
setMonth(m);
setDay(d);
setYear(y);
}
void setMonth(int &m)
{
if (month>0 && month <13)
{
month = m;
}else
throw invalid_argument("month must be 1-12");
}
unsigned int getMonth()const
{
return month;
}
void setDay(int &d)
{
if(day>0 && day<32)
{
day = d;
}else
throw invalid_argument("day must be 0-31");
}
unsigned int getDay()const
{
return day;
}
void setYear(int &y)
{
year = y;
}
unsigned int getYear()const
{
return year;
}
void print()
{
cout<< month <<'/' << day << '/' << year;
}
void nextDay()
{
int numberOfDaysToAdd = 1;
array <int,12> daysInAMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
cout << "the date before is: " << month << "/" << day << "/" << year << endl;
day += numberOfDaysToAdd;
while (day > daysInAMonth [month - 1 ] )
{
day-= daysInAMonth [month - 1 ];
month++;
if (month > 12){
month = 1;
year++;
}
}
cout << "the day after is: " << month << "/" << day << "/" << year << endl;
}
private:
unsigned int month;
unsigned int day;
unsigned int year;
};
int main()
{
char response = 'y';
Date date(12, 3, 2013 );
cout<<"The date is :";
date.print();
cout<<endl;
cout<<endl;
cout<<"do you wish to check the next date(y/n)? :";
cin >> response;
cout<<endl;
while(response == 'y')
{
date.nextDay();
cout<<endl;
cout<<"do you wish to check the next date(y/n)? : ";
cin >> response;
cout<<endl;
}
return 0;
}
You should check the m and d values not the month and day member variables.
void setMonth(int &m)
{
if (month>0 && month <13)
should be
void setMonth(int &m)
{
if (m>0 && m <13)

c++ trouble with either RTTI or binary file io

I think I am having trouble with binary file io. If I run my program, create some employee objects and then display them everything works fine. If I save the object data and reload the program I get an RTTI exception. It apears to me that my LoadEmployeeData() and Savelist(vector &e) functions work just fine. The exception occurs in my DisplayEmployeeData() function when I try to use typeid.
Just to reiterate, I am getting an RTTI error when using typeid on an object loaded from disk.
//****************header file***********
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <typeinfo>
#include <ctime>
#include <cstdlib>
using namespace std;
class Employee
{
private:
int employeeID;
char name[80];
int SSN;
public:
Employee();
Employee(int, char*,int);
virtual ~Employee();
virtual void DisplayBaseData();
//getters
int GetID();
char* getName();
int GetSSN();
//setters
void SetID(int);
void SetName(char*);
void SetSSN(int);
};//end Employee class
class Salary : public Employee
{
private:
double salary;
public:
Salary();
Salary(int, char*, int, double); //id, name, ssn, salary
~Salary();
void DisplayEmployeeData();
//getters
double GetSalary();
//setters
void SetSalary(double);
};//end class Exempt
class Hourly : public Employee
{
private:
double rate;
double hoursWorked;
public:
Hourly();
Hourly(int, char*, int, double, double); //id, name, ssn, rate
~Hourly();
void DisplayEmployeeData();
//getters
double GetRate();
double GetHoursWorked();
//setters
void SetRate(double);
void SetHoursWorked(double);
};//end Hourly Class
const int HOURLYTYPE = 0;
const int SALARYTYPE = 1;
//*******body*******
#include "lab05.h";
Employee::Employee(){};
Employee::Employee(int ID, char* nme, int ssn) : employeeID(ID), SSN(ssn)
{
strcpy(name, nme);
}
int Employee::GetID()
{
return employeeID;
}
char* Employee::getName()
{
return name;
}
int Employee::GetSSN()
{
return SSN;
}
void Employee::SetID(int i)
{
employeeID = i;
}
void Employee::SetName(char* n)
{
strcpy(name, n);
}
void Employee::SetSSN(int i)
{
SSN = i;
}
void Employee::DisplayBaseData()
{
cout << "ID: \t" << employeeID << endl;
cout << "Name: \t " << name << endl;
cout << "SSN: \t" << SSN << endl;
}
Employee::~Employee(){}
Salary::Salary(){}
Salary::Salary(int id, char* nme, int ssn, double slry) : Employee(id, nme, ssn), salary(slry){}
void Salary::DisplayEmployeeData()
{
DisplayBaseData();
cout << "Salary: \t " << salary << endl;
}
double Salary::GetSalary()
{
return salary;
}
void Salary::SetSalary(double d)
{
salary = d;
}
Salary::~Salary(){}
Hourly::Hourly(){}
Hourly::Hourly(int id, char* nme, int ssn, double rte, double worked) : Employee(id, nme, ssn), rate(rte), hoursWorked(worked){}
void Hourly::DisplayEmployeeData()
{
DisplayBaseData();
cout << "Rate: \t" << rate << endl;
cout << "Worked: \t " << hoursWorked << endl;
}
double Hourly::GetRate()
{
return rate;
}
double Hourly::GetHoursWorked()
{
return hoursWorked;
}
void Hourly::SetRate(double d)
{
rate = d;
}
void Hourly::SetHoursWorked(double d)
{
hoursWorked = d;
}
Hourly::~Hourly(){}
vector<Employee*> LoadEmployeeData()
{
vector<Employee*> employeeList;
string fileName = "";
cout << "\nEnter filename for employee data: ";
cin >> fileName;
fstream file;
file.open(fileName, ios::in, ios::binary);
char buffer[4096] = {0};
int numEntries;
file.read((char*)&numEntries, sizeof(int));
cout << numEntries << " number of entries found." << endl;
if (numEntries != 0)
{
int identifier;
for (int i = 0; i < numEntries; i++)
{
file.read((char*)&identifier, sizeof(int));
if (identifier == SALARYTYPE)
{
Employee* temp = new Salary();
file.read((char*)temp, sizeof(Salary));
employeeList.push_back(temp);
}
else if (identifier == HOURLYTYPE)
{
Employee* temp = new Hourly();
file.read((char*)temp, sizeof(Hourly));
employeeList.push_back(temp);
}
}
}
else cout << "No Entries found." << endl;
file.close();
return employeeList;
}//end LoadEmployeeData function
void ListEmployees(vector<Employee*> &e)
{
if (e.size() != 0)
{
for (int i = 0; i < e.size(); i++)
{
if (typeid(*(e[i])) == typeid(Hourly))
{
cout << "\n(" << i << ")" << endl;
dynamic_cast<Hourly*>(e[i])->DisplayEmployeeData();
}
else if (typeid(*(e[i])) == typeid(Salary))
{
cout << "\n(" << i << ")" << endl;
dynamic_cast<Salary*>(e[i])->DisplayEmployeeData();
}
}
}
else cout << "No items in list" << endl;
}// end ListEmployees function
void ModifyEmployee(vector<Employee*> &e)
{
cout << "Enter employee selection." << endl;
}
void CreateEmployee(vector<Employee*> &e)
{
bool continueLoop = true;
srand(time(0)); //seed random number generator
cout << "\n Enter new employee information." << endl;
cout << "Name: ";
char newName[80] = {0};
cin >> newName;
cout << "\n SSN: ";
int newSSN;
cin >> newSSN;
char newType = '-1';
do
{
cout << "\n Is new employee paid a (s)alary or (h)ourly rate? ";
cin >> newType;
if (newType == 's' || newType == 'h') continueLoop = false;
else cout << "incorrect input" << endl;
}while (continueLoop == true);
if (newType == 's')
{
cout << "Enter salary amount: ";
double amount;
cin >> amount;
e.push_back(new Salary(rand() % 1000 + 1, newName, newSSN, amount));
}
else if (newType == 'h')
{
cout << "Enter hourly amount: ";
double amount;
cin >> amount;
cout << "Enter hours worked: ";
double hoursWorked;
cin >> hoursWorked;
e.push_back(new Hourly(rand() % 1000 + 1, newName, newSSN, amount, hoursWorked));
}
}
void Savelist(vector<Employee*> &e)
{
if (e.size() == 0)
cout << "No employees in list. Nothing done." << endl;
else
{
cout << "Enter save filename: ";
char fileName[80] = {'\0'};
cin >> fileName;
fstream* file = new fstream();
file->open(fileName, ios::out, ios::binary);
char buffer[80] = {'\0'};
int numEntries = e.size();
file->write((char*)&numEntries, sizeof(int)); //writes number of entries
for (int i = 0; i < e.size(); i++)
{
if (typeid(*e[i]) == typeid(Salary))
{
int classType = SALARYTYPE;
file->write((char*)&classType, sizeof(int));
file->write((char*)dynamic_cast<Salary*>(e[i]), sizeof(Salary));
}
else if (typeid(*e[i]) == typeid(Hourly))
{
int classType = HOURLYTYPE;
file->write((char*)&classType, sizeof(int));
file->write((char*)dynamic_cast<Hourly*>(e[i]), sizeof(Salary));
}
}
file->close();
}
}
void DeleteEmployee(vector<Employee*> &e)
{
cout << "Input index number of employee to delete: ";
int idx = 0;
cin >> idx;
if (idx > e.size() -1)
cout << "invalid index number\n" << endl;
else
{
delete e[idx];
e.erase(e.begin() + idx); //removes from list
}
}
int main()
{
const int ZERO = 0;
const int ONE = 1;
const int TWO = 2;
const int THREE = 3;
const int FOUR = 4;
const int FIVE = 5;
const int SIX = 6;
int exitMainLoop = false; //for flow control
int mainMenuChoice = -1;
vector<Employee*> employeeList;
do
{
cout << "Select from the following options." << endl;
cout << "(1) Load employee data file." << endl;
cout << "(2) View Employees." << endl;
cout << "(3) Modify Employee data. " << endl;
cout << "(4) Create new employee." << endl;
cout << "(5) Save list to file." << endl;
cout << "(6) Delete employee data. " << endl;
cout << "(0) Exit program." << endl;
//add more options
cout << "Enter selection: ";
cin >> mainMenuChoice;
if (cin.fail())
{
cout << "\nInvalid selection. Try again" << endl;
cin.clear();
string garbage = "";
cin >> garbage;
}
else if (mainMenuChoice == ONE)
employeeList = LoadEmployeeData();
else if (mainMenuChoice == TWO)
ListEmployees(employeeList);
else if (mainMenuChoice == THREE)
ModifyEmployee(employeeList);
else if (mainMenuChoice == FOUR)
CreateEmployee(employeeList);
else if (mainMenuChoice == FIVE)
Savelist(employeeList);
else if (mainMenuChoice == SIX)
DeleteEmployee(employeeList);
else if (mainMenuChoice == ZERO)
exitMainLoop = true;
}while(exitMainLoop == false);
system("PAUSE");
}
You can't read/write raw C++ objects from/to disk if they have virtual methods (or use RTTI, which requires virtual methods) because there's no guarantee that the vtable address from the first execution will be written to disk, and there's no guarantee that the vtable will be in the same place the next time the program is run -- hence, the address that was written to disk will point somewhere incorrect when it is read back.
file->write((char*)dynamic_cast<Hourly*>(e[i]), sizeof(Salary));
looks suspicious. did you mean sizeof(Hourly)?