wrong in sort function

Hi,
This Program shows total salary for each employee but after sorting in sort function It shows dumb salaries.
where is wrong in sort function?
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
#include <conio.h>
#include <iostream>
using namespace std;
const int n=1;
const int m=1;
const int a=m+n;

////////////////////////////////////////////


/////////////////////////////////       Base Class :


class Employee
{
protected:
	char name[30];
	int id_number;
	
public:
	void input_Information();
};
void Employee::input_Information()
{
	cout<<"Enter First And Last Name:";
	cin.get(name,20);
	cout<<"Enter ID Number:";
	cin>>id_number;
		cin.get();
}


///////////////////////
//////////////////////
////////////////////// Derived Class 1:


class Contractor:public Employee
{
protected:
	char type[30];
	float Base_Contractor,Salary_Per_Hour_Contractor;
	int	TaxRate_Contractor,Overtime_Hours;
public:
	float Total_Salary;
		void input_Contractor();
		void calc_Contractor();
};
void Contractor::input_Contractor()
{
	char type[]="Contractor Wage";
	cout<<"Enter Base Salary (For Contractor):";
	cin>>Base_Contractor;
		cin.get();
	cout<<"Enter Salary Per Hour (For Contractor):";
	cin>>Salary_Per_Hour_Contractor;
		cin.get();
	cout<<"Enter Tax Rate (For Contractor):";
	cin>>TaxRate_Contractor;
		cin.get();
	cout<<"Enter OverTime Hour (For Contractor):";
	cin>>Overtime_Hours;
		cin.get();
}
void Contractor::calc_Contractor()
{
	
	Total_Salary=(Base_Contractor + (Salary_Per_Hour_Contractor*Overtime_Hours))- ((Base_Contractor + (Salary_Per_Hour_Contractor*Overtime_Hours))*TaxRate_Contractor/100);
	cout<<Total_Salary<<endl;
}


//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////  //////////////////////// Derived Class 2:


class Hourly_Employee :public Contractor
{
	char type[30];
	int NumberOf_Work_Hours,Absent_Hours;
	float Salary_Per_Hour,Total_Salary;
public:
	void input_Hourly_Employee();
	void calc_Hourly_Employee();
	void sort(Hourly_Employee obj2[],int n);
};
void Hourly_Employee::input_Hourly_Employee()
{
    char type[]="Hourly wage";
	cout<<"Enter Number Of Work Hours (For Hourly):";
	cin>>NumberOf_Work_Hours;
		cin.get();
	cout<<"Enter Absent Hours (For Hourly):";
	cin>>Absent_Hours;
		cin.get();
	cout<<"Enter Your Salary Per Hour (For Hourly):";
	cin>>Salary_Per_Hour;
		cin.get();
}
void Hourly_Employee::calc_Hourly_Employee()
{
	Total_Salary=(NumberOf_Work_Hours*Salary_Per_Hour)-(Absent_Hours*Salary_Per_Hour);
	cout<<Total_Salary<<endl;
}
void Hourly_Employee::sort(Hourly_Employee obj2[] ,int a)
{
	Hourly_Employee temp;
	for (int j=0;j<a;j++)
	{
		for (int i=0;i<a-1;i++)
		{
			if (obj2[i].Total_Salary>obj2[i+1].Total_Salary)
			{
				temp=obj2[i];
			obj2[i]=obj2[i+1];
			obj2[i+1]=temp;
			}
		}
	}
	for (int i=0;i<a;i++)
	{
		cout<<obj2[i].type[20]<<" "<<obj2[i].name[20]<<" "<<"Total Salary:"<<obj2[i].Total_Salary<<endl;
	}
}



//////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////


int main()
{
	Hourly_Employee myobj;
	Hourly_Employee obj2[a];
	for (int i=0;i<n;i++)
	{
		obj2[i].input_Information();
		obj2[i].input_Contractor();
		obj2[i].calc_Contractor();
	}
	for (int i=m;i<a;i++)
	{
		obj2[i].input_Information();
		obj2[i].input_Hourly_Employee();
		obj2[i].calc_Hourly_Employee();
	}
	myobj.sort(obj2,a);
	cin.get();
	cin.get();
}
Last edited on
please provide test case
What's your meaning of test case?
An example input with the expected output
What's your meaning of test case?

maybe what he/she mean that something out of your program expectations, e.g. some errors or bugs...
I edited code in english and changed something.
It's a image from input and output:
http://upcity.ir/images/24250594053095050732.jpg
You have duplicated members.
obj2[i].calc_Contractor(); calculates Contractor::Total_Salary but you are operating on Hourly_Employee::Total_Salary.

Apart, {Contractor,Hourly_Employee}::type is not used.
1
2
3
cout<<obj2[i].type[20] //prints just 1 character
   <<" "<<obj2[i].name[20] //same
   <<" "<<"Total Salary:"<<obj2[i].Total_Salary<<endl;


myobj.sort(obj2,a); that's a flag for bad code; You need an object to perform that operation, but any object is good.
I defined a char array (20 character) in Derived Class 1
1
2
protected:
	char type[30];

and then in input function I wrote it:
type[]="Contractor Employee";
When I wrote It gives a error :
expected an expression
Why does it give error?
Last edited on
Because you can't initialize it that way.
May use strcpy() or make it a virtual function.
Topic archived. No new replies allowed.