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 ¤tDate, 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. " <<
¤tDate << ", 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 ¤tDate, 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
|