I have to write a program which uses 2 classes to load 2 dat files into arrays. I then have to somehow compare the IDs in array1 to the IDs in array2 to get the extra IDs out of array1. These extra IDs then have to be stored somewhere so they can be printed out at the end of the program. I also have to get these 2 classes to communicate somehow so it can use the hours from array2 and the pay from array1 to come up with a total pay amount. Any hints or clues on how to do any of that would be greatly appreciated. I already have the parts of the class for loading, sorting, and searching the arrays written. I just don't know how to make the 2 classes communicate.

Recommended Answers

All 4 Replies

>>. I also have to get these 2 classes to communicate somehow so it can use the hours from array2 and the pay from array1 to come up with a total pay amount.
>>I just don't know how to make the 2 classes communicate.

There are couple of ways.

But I suggest you make a function, that calculates the total pay amount.

float calculateTotalPay(const ClassOne& pay, const ClassTwo& rate){...}

Okay let me see if I can explain this better. Brain wasn't functioning so well last night. Class1 loads the info.dat file into parallel arrays for IDs and pay. Class2 loads the pay.dat file into parallel arrays for IDs and hours. I think my biggest problem here is I don't know how to make those 2 classes communicate.

I have to check the IDs in class2 against the IDs in class1 to determine if any of the IDs in class2 should not be there. Any bad IDs found are to be displayed on the screen and placed somewhere they can be called up later to display all together. Figure I have to do that bit with another array. When IDs are found that are valid I have to use the hours from Class2 and the pay from Class1 to get the total pay for that person and I then need to store that information somewhere so it can be called up later. I don't know how to write the code to check the IDs against each other though.

After it's all said and done I have to display on screen the valid IDs with their total pay, then total number of people paid and the total amount paid to everyone. Then after that I have to display the list of bad IDs.

Most programs that I've had to do I just had to figure out one thing and the rest of it clicked. I think that if I could figure out how to make the classes communicate it would be a huge help.

I suppose this might help. Classes so far.

class ClassOne
{
	//Data declarations
	private:
		int emIds[500];
		double rate[500];
		int idCount;

	//Methods declarations
	public:
		ClassOne();
		void loadArrays();
		double getRate(int);
		int binSearch(int);
		void bubbleSort();
};

ClassOne::ClassOne()
{
	idCount = 0;
	for(int ct = 0; ct < 500; ++ct) emIds[ct] = 0;
}

void ClassOne::loadArrays()
{
	ifstream employeeIn;
	employeeIn.open("info.dat");
	if(employeeIn.is_open())
	{
		employeeIn >> emIds[idCount]
				   >> rate[idCount];
		while(!employeeIn.eof())
		{
			++idCount;
			employeeIn >> emIds[idCount]
					   >> rate[idCount];
		}
		employeeIn.close();
	}
	else
	{
		idCount = -1;
		cout << "File failed to open.";
	}
}

double ClassOne::getRate(int location)
{
	return rate[location];
}

int ClassOne::binSearch(int empId)
{
	int first, last, mid, found;
	first = 0;
	last = idCount - 1;
	found = 0;

	while(first <= last && found == 0)
	{
		mid = (first + last) / 2;
		if(emIds[mid] == empId)
			found = 1;
		else if(emIds[mid] < empId)
			first = mid + 1;
		else if(emIds[mid] > empId)
			last = mid - 1;
	}
	if(found == 0)
		mid = -1;
	return mid;
}

void ClassOne::bubbleSort()
{
	int tempId, swap, index, last = idCount -1;
	double tempRate;
	while(last > 0)
	{
		swap = 0;
		index = 0;
		while(index < last)
		{
			if(emIds[index] > emIds[index+1])
			{
				tempId = emIds[index];
				emIds[index] = emIds[index+1];
				emIds[index+1] = tempId;

				tempRate = rate[index];
				rate[index] = rate[index+1];
				rate[index+1] = tempRate;
				swap = 1;
			}
			++index;
		}
		if(swap == 0)
			last = 0;
		else if(swap == 1)
			last = last-1;
	}
}
class ClassTwo
{
	//Data declarations
	private:
		int ids[500];
		double hours[500];
		int idCount;

	//Methods declarations
	public:
		ClassTwo();
		void loadPayArrays();
		void bubbleSort();
};

ClassTwo::ClassTwo()
{
	idCount = 0;
	for(int ct = 0; ct < 500; ++ct) ids[ct] = 0;
}

void ClassTwo::loadPayArrays()
{
	ifstream employeeIn;
	employeeIn.open("pay.dat");
	if(employeeIn.is_open())
	{
		employeeIn >> ids[empCount]
				   >> hours[empCount];
		while(!employeeIn.eof())
		{
			++idCount;
			employeeIn >> ids[empCount]
					   >> hours[empCount];
		}
		employeeIn.close();
	}
	else
	{
		idCount = -1;
		cout << "File failed to open.";
	}
}

void ClassTwo::bubbleSort()
{
	int tempId, swap, index, last = idCount -1;
	double tempHours;
	while(last > 0)
	{
		swap = 0;
		index = 0;
		while(index < last)
		{
			if(ids[index] > empPayNumbers[index+1])
			{
				tempId = ids[index];
				ids[index] =  ids[index+1];
				ids[index+1] = tempId;

				tempHours = hours[index];
				hours[index] = hours[index+1];
				hours[index+1] = tempHours;
				swap = 1;
			}
			++index;
		}
		if(swap == 0)
			last = 0;
		else if(swap == 1)
			last = last-1;
	}
}

Class2 bubbleSort that should be ids[index+1]
And apparently I need to alter my binary search method. Hoping I figure that out soon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.