I am trying to track down errors

This inheritance problem compiles and runs, however, the output is not right and I am having a hard time tracking down the problem. Can anyone point in the right direction. Here is the code:
[code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
[code][/c[co[code]#include <iostream >
using namespace std;
// Member function definitions for Date class.
class Date
{
public:
Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;
};
void Date::setDate()
{
int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr >= 1900 && yr <= 2100 ? yr : 1990;
day = checkDay( dy ); // validate the day
}

// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for possible leap year
testDay == 29 &&
( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";

return 1; // leave object in consistent state if bad value
}

// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
class Employee
{
private:
int ID;

public:
char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtual double getMonthsPay() = 0;
};


Employee::Employee(int id)
{
ID=id;
cout<<"Enter name:";
cin>>name;
//fflush(stdin);
cout<<"Enter Address:";
cin>>address;
cout<<"Enter phone no:";
cin>>phone;
birthDate.setDate();
hireDate.setDate();
}
int Employee::GetID()
{
return ID;
}

class SalariedEmployee : public Employee
{
public:
double salary;
double getMonthsPay();
SalariedEmployee(int id);
};
SalariedEmployee::SalariedEmployee(int id):Employee(id)
{
if(id!=0){
cout<<"Enter salary:";
cin>>salary;}
}
double SalariedEmployee::getMonthsPay()
{
return salary;
}



class HourlyEmployee : public Employee
{
public:
double hours;
double hourlyRate;
double getMonthsPay();
HourlyEmployee(int id);
};
HourlyEmployee::HourlyEmployee(int id):Employee(id)
{
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}

/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{
public:
double getMonthsPay();
OutsourcedEmployee();
};
OutsourcedEmployee::OutsourcedEmployee():SalariedEmployee(0)
{}
double OutsourcedEmployee::getMonthsPay()
{
return 20;
}



void main()
{
//Hourly Employee class
cout<<"Hourly Employee class"; //creating object
HourlyEmployee emp1(23);
//outputting employee datails
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"<< //Saleried employee class
cout<<"Salaried employee class"; //creating object
SalariedEmployee emp2(143);
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"; //Outsourced Employee class
OutsourcedEmployee emp3;
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp3.hireDate.print();
cout<<"PAY:"<< //pause system for a while
system("pause");

}//end main 
How do you know the output is not right? What were you expecting?

Also, one of the main ways to track down bugs is to make your code as clear and readable as possible. You might consider using some indentation and leaving the occasional gap between blocks of code. That will make finding errors easier for you, and for those you ask to help.
hello dear hallux2003

see the code , I tried to fix the error



#include <iostream >
using namespace std;
// Member function definitions for Date class.
class Date
{
public:
Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;
};
void Date::setDate()
{
int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr >= 1900 && yr <= 2100 ? yr : 1990;
day = checkDay( dy ); // validate the day
}

// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for possible leap year
testDay == 29 &&
( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";

return 1; // leave object in consistent state if bad value
}

// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
class Employee
{
private:
int ID;

public:
char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtual double getMonthsPay() = 0;
};


Employee::Employee(int id)
{
ID=id;
cout<<"Enter name:";
cin>>name;
//fflush(stdin);
cout<<"Enter Address:";
cin>>address;
cout<<"Enter phone no:";
cin>>phone;
birthDate.setDate();
hireDate.setDate();
}
int Employee::GetID()
{
return ID;
}

class SalariedEmployee : public Employee
{
public:
double salary;
double getMonthsPay();
SalariedEmployee(int id);
};
SalariedEmployee::SalariedEmployee(int id):Employee(id)
{
if(id!=0){
cout<<"Enter salary:";
cin>>salary;}
}
double SalariedEmployee::getMonthsPay()
{
return salary;
}



class HourlyEmployee : public Employee
{
public:
double hours;
double hourlyRate;
double getMonthsPay();
HourlyEmployee(int id);
};
HourlyEmployee::HourlyEmployee(int id):Employee(id)
{
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}

/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{
public:
double getMonthsPay();
OutsourcedEmployee();
};
OutsourcedEmployee::OutsourcedEmployee():SalariedEmployee(0)
{}
double OutsourcedEmployee::getMonthsPay()
{
return 20;
}



void main()
{
//Hourly Employee class
cout<<"Hourly Employee class"; //creating object
HourlyEmployee emp1(23);
//outputting employee datails
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"
<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"<< //Saleried employee class
cout<<"Salaried employee class"; //creating object
SalariedEmployee emp2(143);
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"; //Outsourced Employee class
OutsourcedEmployee emp3;
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp3.hireDate.print();
cout<<"PAY:"<< //pause system for a while
system("pause");

}//end main

This is the output I get
Hourly Employee classEnter name:Brian
Enter Address:1234 glijl
Enter phone no:Enter day:19
Enter month:12
Enter year2010
Enter day:14
Enter month:10
Enter year2010
Enter number of hours:40
Enter hourly pay rate:13.00
[b]ID:6486CACCName:6486CACCAddress:6486CACCPhone:6486CACCHireDate:10/14/2010PAY:648
6CACCSalaried employee classEnter name:
[/b]

what about the output , is it that you want or not ?
This part is where the problem is Name, Adress Phone Pay
After you hit enter on hourly pay.


[b]ID:6486CACCName:6486CACCAddress:6486CACCPhone:6486CACCHireDate:10/14/2010PAY:648
6CACCSalaried employee classEnter name:

now I understand your question :)

I'll tried to see it again and reply

I have gone through it and I am stumped. This is all new to me.
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";

EDIT: also, cout<<"PAY:"<< should be cout<<"PAY:";

And it's good practice to avoid system() calls: http://www.cplusplus.com/forum/articles/11153/
Last edited on
That helped a lot something is still wrong I think I have somehing wrong in the calculations:

1
2
3
4
5
6
7
8
9
10
11
HourlyEmployee::HourlyEmployee(int id):Employee(id)
{
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}


salam hallux

today I go through your program, actually which you do is almost correct but you need to add
some organizing to your program to be clear to the user.

As filipe said you need to be carefull to that points which he mentioned above.

Also you need to check your calculations and that formulas which you use.

Generally, I added some hints on your program to be easy to you to fix it, try to do your best,
if you give up I'll help you.

all best :
Bushra



#include <iostream >
using namespace std;

// Member function definitions for Date class.
class Date
{

public:

Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format

private:

int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;

};

void Date::setDate()
{

int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year:";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}

//year = yr >= 1900 && yr <= 2100 ? yr : 1990;

if(yr>=1900 && yr<=2100)

year=yr;

day = checkDay( dy ); // validate the day
}

// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;

if ( (month == 2) && // February: Check for possible leap year
(testDay == 29) &&
( year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0 ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";

return 1; // leave object in consistent state if bad value
}

// Print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year<<endl;
}

class Employee
{

private:
int ID;

public:

char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtual double getMonthsPay() = 0;
};


Employee::Employee(int id)
{

ID=id;
cout<<"Enter name:";
cin>>name;
//fflush(stdin);
cout<<"Enter Address:";
cin>>address;
cout<<"Enter phone no:";
cin>>phone;
birthDate.setDate();
hireDate.setDate();

}

int Employee::GetID()
{

return ID;

}

class SalariedEmployee : public Employee
{

public:

double salary;
double getMonthsPay();
SalariedEmployee(int id);

};

SalariedEmployee::SalariedEmployee(int id):Employee(id)
{

if(id!=0)
{
cout<<"Enter salary:";
cin>>salary;
cout<<endl;
}

}

double SalariedEmployee::getMonthsPay()
{

return salary;

}



class HourlyEmployee : public Employee
{

public:

double hours;
double hourlyRate;
double getMonthsPay();
HourlyEmployee(int id);

};

HourlyEmployee::HourlyEmployee(int id):Employee(id)
{

cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
cout<<endl;

}

double HourlyEmployee::getMonthsPay()
{

return hours*hourlyRate;

}

/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{

public:
double getMonthsPay();
OutsourcedEmployee();
};

OutsourcedEmployee::OutsourcedEmployee():SalariedEmployee(0)
{}

double OutsourcedEmployee::getMonthsPay()
{

return 20;

}



void main()
{

//Hourly Employee class
cout<<"Hourly Employee class"; //creating object
cout<<endl<<endl;
HourlyEmployee emp1(23);
//outputting employee datails

cout<<"ID:"<<endl;
cout<<"Name:"<<endl;
cout<<"Address:"<<endl;
cout<<"Phone:"<<endl;
cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"<<endl<<endl; //Saleried employee class
cout<<"Salaried employee class"<<endl; //creating object
cout<<endl;
SalariedEmployee emp2(143);

cout<<"ID:"<<endl;
cout<<"Name:"<<endl;
cout<<"Address:"<<endl;
cout<<"Phone:"<<endl;
cout<<"HireDate:";

emp1.hireDate.print();
cout<<"PAY:"<<endl<<endl; //Outsourced Employee class

OutsourcedEmployee emp3;

cout<<endl;
cout<<"ID:"<<endl;
cout<<"Name:"<<endl;
cout<<"Address:"<<endl;
cout<<"Phone:"<<endl;
cout<<"HireDate:";

emp3.hireDate.print();
cout<<"PAY:"<<endl<<endl; //pause system for a while

system("pause");

}//end main
Topic archived. No new replies allowed.