Google Answers Logo
View Question
 
Q: Help with intro C++ homework assignment ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Help with intro C++ homework assignment
Category: Computers > Programming
Asked by: krwilkins-ga
List Price: $10.00
Posted: 03 Dec 2002 17:00 PST
Expires: 02 Jan 2003 17:00 PST
Question ID: 118759
I'd like help converting exisiting C++ code into two functions, one
value returning and another one that passes two variables by
reference. I am pasting the existing code below. The program does
payroll calculations.

//Renee Wilkins					M-W 7:30 PM				December 1, 2002
//Lab 4 - Joe's Electronics Payroll Program

# include <iostream>
# include <cmath>
# include <fstream>
# include <string>
using namespace std;

//function prototype one
void pageHeader();


//funtion prototype two



//function prototype three


//declare global variables
	string studentName   = " ";
	int currentDate      = 0;


main()
{
	//declare variables
	
	int x                    = 0;
	int employeeNumber[15]   = {101, 102, 103, 104, 106, 108, 112, 116,
125, 131, 133, 147, 168, 172, 201};
	int departmentNumber[15] = {10, 12, 17, 16, 12, 10, 16, 17, 12, 12,
17, 12, 10, 10, 16};
	float hourlyRate[15]     = {15.00, 15.60, 14.00, 16.25, 13.75, 14.25,
14.75, 15.25, 16.60, 18.50, 15.25, 16.00, 13.50, 14.90, 13.75};
	float hoursWorked[15]    = {40, 45, 39, (float)41.5, 40, 44, 42, 40,
(float)40.25, 40, (float)40.75, 40, 20, 40, 25};
	float bonusRate[15]      = {(float)0.03, (float)0.04, (float)0.05,
(float)0.01, (float)0.04, (float)0.03, (float)0.01, (float)0.05,
(float)0.04, (float)0.04, (float)0.05, (float)0.04, (float)0.03,
(float)0.03, (float)0.01};
	float regularPay[15]     = {(float)0.0};
	float overHours[15]      = {(float)0.0};
	float overtimePay[15]    = {(float)0.0};
	float bonusAmount[15]    = {(float)0.0};
	float totalPay[15]       = {(float)0.0};
	float totalHourly        = 0.0;
	float totalRegPay        = 0.0;
	float totalOvertimePay   = 0.0;
	float totalBonus         = 0.0;
	float totalTotal         = 0.0;



	//open output file
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);

	//enter input items

	cout << "Enter your name: ";
	getline(cin, studentName);
	cout << "Enter the current date, December ";
	cin >> currentDate;
	

	//**************function call for pageHeader********************
	pageHeader();
	
	while (x < 15)
	{ 
		

		if (hoursWorked[x] > 40)
		{
			
			overHours[x] = hoursWorked[x] - 40;
			regularPay[x] = hourlyRate[x] * (hoursWorked[x]- overHours[x]);
			overtimePay[x] = overHours[x] * hourlyRate[x] * 1.5;
		}
		
		else
			regularPay[x] = hourlyRate[x] * hoursWorked[x];
		
		// end if

		if (hoursWorked[x] >= 30)
			bonusAmount[x] = regularPay[x] * bonusRate[x];
		else
			bonusAmount[x] = 0;
		// end if

		
		totalPay[x] = regularPay[x] + overtimePay[x] + bonusAmount[x];

	//update accumulators
	totalHourly = totalHourly + hourlyRate[x];
	totalRegPay = totalRegPay + regularPay [x];
	totalOvertimePay = totalOvertimePay + overtimePay[x];
	totalBonus = totalBonus + bonusAmount[x];
	totalTotal = totalTotal + totalPay[x];
			
	
	outFile << fixed;
	outFile.precision(2);
	outFile << employeeNumber[x] << "        "<< departmentNumber[x] << "
    $"<< hourlyRate[x] << "    "<< hoursWorked[x] << "    $"<<
regularPay[x] << "   $"<< overtimePay[x] << "     $"<< bonusAmount[x]
<< "   $"<< totalPay[x] << endl;


	
	//increment counter
	x = x + 1;

	}

	//send accumulated values to output file
	outFile << fixed;
	outFile.precision(2);
	outFile << "               " << endl;
	outFile << " Totals           $" << totalHourly << "           $" <<
totalRegPay << "  $" << totalOvertimePay << "    $" << totalBonus << "
  $" << totalTotal << endl;
	

	return 0;

}


	

//***********void function used to add page headings************
void pageHeader()
{
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);
	outFile << fixed;
	outFile.precision(2);

	outFile << "Joe's Electronic Store        " << "Dec. " << currentDate
<< ", 2002" << "      Sales Person:  " << studentName << endl;
	outFile << "  " << endl;
	
	outFile << "Employee  " << "Dept.   " << "$Hourly   " << "Hours    "
<< "$Gross   " << "$Overtime  " << "        " << "$Total" << endl;
	
	outFile << "Number    " << "Num     " << " Rate     " << "Worked   "
<< "  Pay    " << "   Pay     " << "$Bonus  " << "  Pay " << endl;
	
	outFile << "--------  " << "-----   " << "-------   " << "------   "
<< "------   " << "---------  " << "------  " << "------" << endl;

} // end pageHeader function	


//**********value returning function ************


//**********function that passes two variable by
reference***************

Request for Question Clarification by hammer-ga on 03 Dec 2002 17:16 PST
More detail is needed to answer your question.

1. The routine that passes two variables: What variables is it
supposed to pass? What is it supposed to pass them to?

2. The routine that returns: What does it return? Where is it supposed
to return to? What does it do?

We need a description along the following lines:
*******************
Convert the code below into two functions. The first function reads
the studentName and currentDate from stdin. It then passes these
values by reference to the second function, which performs all the
processing, writes the output file and returns success or failure to
the calling function.********************

Without something like the above, we simply don't know what to write
for you.

- Hammer

Clarification of Question by krwilkins-ga on 03 Dec 2002 17:53 PST
Our instructions for what part of the program to put into the fuctions
were not specific. I am trying to put the regular pay calculation into
a value returning function. I will paste the code again. I have one
error where I attempt to call the function. I thought it would be good
(easiest) to pass the student name and the current date variables by
reference.

# include <iostream>
# include <cmath>
# include <fstream>
# include <string>
using namespace std;

//function prototype one
void pageHeader();


//funtion prototype two
float regularPay(float [], float []);



//function prototype three


//declare global variables
	string studentName       = " ";
	int currentDate          = 0;
	int x                    = 0;
	float hourlyRate[15]     = {15.00, 15.60, 14.00, 16.25, 13.75, 14.25,
14.75, 15.25, 16.60, 18.50, 15.25, 16.00, 13.50, 14.90, 13.75};
	float hoursWorked[15]    = {40, 45, 39, (float)41.5, 40, 44, 42, 40,
(float)40.25, 40, (float)40.75, 40, 20, 40, 25};



main()
{
	//declare variables
	
	
	int employeeNumber[15]   = {101, 102, 103, 104, 106, 108, 112, 116,
125, 131, 133, 147, 168, 172, 201};
	int departmentNumber[15] = {10, 12, 17, 16, 12, 10, 16, 17, 12, 12,
17, 12, 10, 10, 16};
	float bonusRate[15]      = {(float)0.03, (float)0.04, (float)0.05,
(float)0.01, (float)0.04, (float)0.03, (float)0.01, (float)0.05,
(float)0.04, (float)0.04, (float)0.05, (float)0.04, (float)0.03,
(float)0.03, (float)0.01};
	float regularPay[15]     = {(float)0.0};
	float overHours[15]      = {(float)0.0};
	float overtimePay[15]    = {(float)0.0};
	float bonusAmount[15]    = {(float)0.0};
	float totalPay[15]       = {(float)0.0};
	float totalHourly        = 0.0;
	float totalRegPay        = 0.0;
	float totalOvertimePay   = 0.0;
	float totalBonus         = 0.0;
	float totalTotal         = 0.0;



	//open output file
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);

	//enter input items

	cout << "Enter your name: ";
	getline(cin, studentName);
	cout << "Enter the current date, December ";
	cin >> currentDate;
	

	//**************function call for pageHeader********************
	pageHeader();
	
	while (x < 15)
	{ 
		

		if (hoursWorked[x] > 40)
		{
			
			overHours[x] = hoursWorked[x] - 40;
			regularPay[x] = hourlyRate[x] * (hoursWorked[x]- overHours[x]);
			overtimePay[x] = overHours[x] * hourlyRate[x] * 1.5;
		}
		
		else
			regularPay[x] = regularPay(hourlyRate, hoursWorked);
		
		// end if

		if (hoursWorked[x] >= 30)
			bonusAmount[x] = regularPay[x] * bonusRate[x];
		else
			bonusAmount[x] = 0;
		// end if

		
		totalPay[x] = regularPay[x] + overtimePay[x] + bonusAmount[x];

	//update accumulators
	totalHourly = totalHourly + hourlyRate[x];
	totalRegPay = totalRegPay + regularPay [x];
	totalOvertimePay = totalOvertimePay + overtimePay[x];
	totalBonus = totalBonus + bonusAmount[x];
	totalTotal = totalTotal + totalPay[x];
			
	
	outFile << fixed;
	outFile.precision(2);
	outFile << employeeNumber[x] << "        "<< departmentNumber[x] << "
    $"<< hourlyRate[x] << "    "<< hoursWorked[x] << "    $"<<
regularPay[x] << "   $"<< overtimePay[x] << "     $"<< bonusAmount[x]
<< "   $"<< totalPay[x] << endl;


	
	//increment counter
	x = x + 1;

	}

	//send accumulated values to output file
	outFile << fixed;
	outFile.precision(2);
	outFile << "               " << endl;
	outFile << " Totals           $" << totalHourly << "           $" <<
totalRegPay << "  $" << totalOvertimePay << "    $" << totalBonus << "
  $" << totalTotal << endl;
	

	return 0;

}


	

//***********void function used to add page headings************
void pageHeader()
{
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);
	outFile << fixed;
	outFile.precision(2);

	outFile << "Joe's Electronic Store        " << "Dec. " << currentDate
<< ", 2002" << "      Sales Person:  " << studentName << endl;
	outFile << "  " << endl;
	
	outFile << "Employee  " << "Dept.   " << "$Hourly   " << "Hours    "
<< "$Gross   " << "$Overtime  " << "        " << "$Total" << endl;
	
	outFile << "Number    " << "Num     " << " Rate     " << "Worked   "
<< "  Pay    " << "   Pay     " << "$Bonus  " << "  Pay " << endl;
	
	outFile << "--------  " << "-----   " << "-------   " << "------   "
<< "------   " << "---------  " << "------  " << "------" << endl;

} // end pageHeader function	


//**********value returning function ************
float regularPay(float hourlyRate[], float hoursWorked[])
{
	//calculate regular pay
	float regPay = 0.0;

	regPay = hourlyRate[x] * hoursWorked[x];

	return regPay;

} //end calculate regular pay

//**********function that passes two variable by
reference***************

Clarification of Question by krwilkins-ga on 03 Dec 2002 18:25 PST
Okay, I have written two functions. I get an error when I try to call
the value-returning function that says it cannot be evaluated as a
function. And the function that passes the current date and student
name by reference does not produce an error, but in the output file
the info shows up in scientific notation or something. Here si the
code...one more time :)

# include <iostream>
# include <cmath>
# include <fstream>
# include <string>
using namespace std;

//function prototype one
void pageHeader();


//funtion prototype two
float regularPay(float [], float []);



//function prototype three
void headerInfo(string &currentDate, string &studentName);

//declare global variables
	string studentName       = " ";
	string currentDate       = " ";
	int x                    = 0;
	float hourlyRate[15]     = {15.00, 15.60, 14.00, 16.25, 13.75, 14.25,
14.75, 15.25, 16.60, 18.50, 15.25, 16.00, 13.50, 14.90, 13.75};
	float hoursWorked[15]    = {40, 45, 39, (float)41.5, 40, 44, 42, 40,
(float)40.25, 40, (float)40.75, 40, 20, 40, 25};



main()
{
	
	//function call for headerInfo
	
	
	//declare variables
	
	
	int employeeNumber[15]   = {101, 102, 103, 104, 106, 108, 112, 116,
125, 131, 133, 147, 168, 172, 201};
	int departmentNumber[15] = {10, 12, 17, 16, 12, 10, 16, 17, 12, 12,
17, 12, 10, 10, 16};
	float bonusRate[15]      = {(float)0.03, (float)0.04, (float)0.05,
(float)0.01, (float)0.04, (float)0.03, (float)0.01, (float)0.05,
(float)0.04, (float)0.04, (float)0.05, (float)0.04, (float)0.03,
(float)0.03, (float)0.01};
	float regularPay[15]     = {(float)0.0};
	float overHours[15]      = {(float)0.0};
	float overtimePay[15]    = {(float)0.0};
	float bonusAmount[15]    = {(float)0.0};
	float totalPay[15]       = {(float)0.0};
	float totalHourly        = 0.0;
	float totalRegPay        = 0.0;
	float totalOvertimePay   = 0.0;
	float totalBonus         = 0.0;
	float totalTotal         = 0.0;
	
	
	//call headerInfo function
	headerInfo(currentDate, studentName);

	//open output file
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);

	//enter input items

	
	

	//**************function call for pageHeader********************
	pageHeader();
	
	while (x < 15)
	{ 
		

		if (hoursWorked[x] > 40)
		{
			
			overHours[x] = hoursWorked[x] - 40;
			regularPay[x] = hourlyRate[x] * (hoursWorked[x]- overHours[x]);
			overtimePay[x] = overHours[x] * hourlyRate[x] * 1.5;
		}
		
		else
			//regularPay[x] = regularPay(hourlyRate, hoursWorked);
			regularPay[x] = hourlyRate[x] * hoursWorked[x];
		
		// end if

		if (hoursWorked[x] >= 30)
			bonusAmount[x] = regularPay[x] * bonusRate[x];
		else
			bonusAmount[x] = 0;
		// end if

		
		totalPay[x] = regularPay[x] + overtimePay[x] + bonusAmount[x];

	//update accumulators
	totalHourly = totalHourly + hourlyRate[x];
	totalRegPay = totalRegPay + regularPay [x];
	totalOvertimePay = totalOvertimePay + overtimePay[x];
	totalBonus = totalBonus + bonusAmount[x];
	totalTotal = totalTotal + totalPay[x];
			
	
	outFile << fixed;
	outFile.precision(2);
	outFile << employeeNumber[x] << "        "<< departmentNumber[x] << "
    $"<< hourlyRate[x] << "    "<< hoursWorked[x] << "    $"<<
regularPay[x] << "   $"<< overtimePay[x] << "     $"<< bonusAmount[x]
<< "   $"<< totalPay[x] << endl;


	
	//increment counter
	x = x + 1;

	}

	//send accumulated values to output file
	outFile << fixed;
	outFile.precision(2);
	outFile << "               " << endl;
	outFile << " Totals           $" << totalHourly << "           $" <<
totalRegPay << "  $" << totalOvertimePay << "    $" << totalBonus << "
  $" << totalTotal << endl;
	

	return 0;

}


	

//***********void function used to add page headings************
void pageHeader()
{
	ofstream outFile;
	outFile.open("Lab4.doc", ios::app);
	outFile << fixed;
	outFile.precision(2);

	outFile << "Joe's Electronic Store        " << "Dec. " <<
&currentDate << ", 2002" << "      Sales Person:  " << &studentName <<
endl;
	outFile << "  " << endl;
	
	outFile << "Employee  " << "Dept.   " << "$Hourly   " << "Hours    "
<< "$Gross   " << "$Overtime  " << "        " << "$Total" << endl;
	
	outFile << "Number    " << "Num     " << " Rate     " << "Worked   "
<< "  Pay    " << "   Pay     " << "$Bonus  " << "  Pay " << endl;
	
	outFile << "--------  " << "-----   " << "-------   " << "------   "
<< "------   " << "---------  " << "------  " << "------" << endl;

} // end pageHeader function	


//**********value returning function ************
float regularPay(float hourlyRate[], float hoursWorked[])
{
	//calculate regular pay
	float regPay = 0.0;

	regPay = hourlyRate[x] * hoursWorked[x];

	return regPay;

} //end calculate regular pay

//**********function that passes two variable by
reference***************
void headerInfo(string &currentDate, string &studentName)
{
	
	//enter input items
	cout << "Enter the current date, December ";
	getline(cin, currentDate);
	cout << "Enter your name: ";
	getline(cin, studentName);
	
} //end enter input items
Answer  
Subject: Re: Help with intro C++ homework assignment
Answered By: efn-ga on 03 Dec 2002 19:55 PST
Rated:5 out of 5 stars
 
Hi krwilkins-ga,

Your value-returning function is named regularPay, but you also have
an array in the main function with exactly the same name.  So when you
tried to call the function, the compiler thought you were referring to
the array, and it complained because calling the array as a function
doesn't make sense.  Choosing different names for the array and the
function should solve that problem.

The problem with the current date and student name is not in the
function that takes parameters by reference, it's in the output
statement:

outFile << "Joe's Electronic Store        " << "Dec. " <<
&currentDate << ", 2002" << "      Sales Person:  " << &studentName <<
endl;

The ampersand ('&') in front of the variable names here means that you
are putting out the addresses of the variables instead of their
values.  You should just use the variable names without the
ampersands, for example, "currentDate" instead of "&currentDate".

I have only attempted to answer your questions here, not debug your
whole program.  It may have other problems, but you may be able to
solve
them yourself.  If you need any more information or further
assistance,
please ask.


Search Strategy:

No search:  personal knowledge and testing with Microsoft Visual C++.


I hope this answer was helpful.

--efn-ga
krwilkins-ga rated this answer:5 out of 5 stars
Answer was explained very nicely for an entry level programmer. Very helpful!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy