I always give positive feedback! Language is C++, please make sure you add clear explanations in the code of what each p

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

I always give positive feedback! Language is C++, please make sure you add clear explanations in the code of what each p

Post by answerhappygod »

I always give positive feedback!
Language is C++, please make sure you add clear
explanations in the code of what each part does, and paste the code
in your answer.
For this problem, you will be ***modifying*** code I
have already written.
To summarize the assignment, you are using the first
character on each input line in the data file to determine a bonus
for each employee on top of the pay I already calculate in my
code.
I have ***already*** done most of what is on the
document, please just add the modifications written in RED in the
document attached.
Here is my code:
/*This program reads formatted input and writing formatted
output.
It reads data one piece at a time from an input file until the end
of the file.
Each line of input is for 1 employee. This program will print a
line of output for each employee in the format shown below
and
then print summary totals at the bottom. This program will also
calculate the weekly pay for each
employee in the input file."
*/
//Including all the required libraries
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdio.h>
//Using standard namespace
using namespace std;
//Calculating pay, separated by employee type
//Employee Type 1
double calcPartTimeHourly(float hours, double hrate) {
if (hours > 39.9) hours = 39.9;
return hours * hrate;
}
//Employee Type 2
double calcPartTimeSalary(float hours, double sal) {
return sal / 52;
}
//Employee Type 3
double calcFullTimeHourlyWithoutOvertime(float hours, double hrate)
{
return hours * hrate;
}
//Employee Type 4
double calcFullTimeHourlyWithOvertime(float hours, double hrate)
{
if (hours <= 40) return hours * hrate;
else return 40 * hrate + (hours - 40) * hrate *
2;
}
//Employee Type 5
double calcFullTimeSalary(float hours, double sal) {
return sal / 52;
}
//MAIN FUNCTION, contains the core function of the program
int main()
{
//Filename
string fname;
//Prompt the user for filename
cout<<"Enter file name: "; cin >>
fname;
//Retrieves the file requested by the user
ifstream ifs (fname);
//Establishes variables for the first
charachter(junk), and the second character (employee type)
char junk, typ;
//Establishes variables for hours worked, and the
salary/hourly rate
float hrs, salOrRate;
//Establishes variables for first, middle, and
last names
string first, middle, last;
//Establishes variable for the employee ID
long empid;
//Establishes variable for the number of
employees
int EmployeeNum = 0;
//Establishes variables for the total amount of
money paid to employees based on the data available
double PayrollNum = 0;
//Establishes the total number of hours worked
across all employees based on the data available
double HoursNum = 0;
//While loop that reads the file until it no longer
detects a junk character (The first character in each line)
while (ifs >> junk)
{
//Pulls the data from the file and
assigns each piece of data to its respective variable
ifs >> typ >> hrs >>
salOrRate >> empid >> first >> middle >>
last;
//Establishes the variable for the
"*" indicator
string indicator = "";
//Establishes the variable for the
amount paid to each employee
double amt;
//Uses the functions that were
already established in order to calculate how much is owed to each
employee
switch (typ) {
//Employee type 1,
PartTime Hourly
case '1':
if (hrs
> 39.9) indicator = "* ";
amt =
calcPartTimeHourly (hrs, salOrRate);
break;
//Employee type 2,
PartTime Salary
case '2':
if (hrs
< 35) indicator = "* ";
amt =
calcPartTimeSalary (hrs, salOrRate);
break;
//Employee type 3,
FullTime Hourly Without Overtime
case '3':
amt =
calcFullTimeHourlyWithoutOvertime (hrs, salOrRate);
break;
//Employee type 4,
Full Time Hourly With Overtime
case '4':
if (hrs
< 40) indicator = "* ";
amt =
calcFullTimeHourlyWithOvertime (hrs, salOrRate);
break;
//Employee type 5,
Full Time Salary
case '5':
if (hrs
< 40) indicator = "* ";
amt =
calcFullTimeSalary (hrs, salOrRate);

break;
}
//Assembles the data and outputs it to
the console for the user to see
cout << empid << setw(3)
<< indicator << setw(15) << last << " "
<< first.substr (0, 1) + " ";
cout << middle.substr (0, 1) + "
$" << fixed << setprecision(2)<<amt <<
endl;
//Counters to keep track of the number
of employees, the number of hours, and the total amount of money
owed to employees
EmployeeNum+=1;
HoursNum+=hrs;
PayrollNum+=amt;
}
//Outputs the total number of employees once the
loop is over for the user to see
printf("Number of Employees: %d\n",EmployeeNum);
//Outputs the total number of hours worked once the
loops is over for the user to see
printf("Number of Hours Worked: %.2f\n",HoursNum);
//Outputs the total amount of money owed to
employees once the loop is over for the user to see
cout << "Total Payroll: $" << fixed
<< setprecision(2)<<floor(PayrollNum * 100.0) / 100.0
<< endl;
ifs.close();
return 0;
}
This program reads data about employees from a text
file, and will use this data to determine how much to pay each
employee.
The format of the data in the file, along with the
guidelines used to calculate payment, are in the images
below.
FORMAT OF THE DATA:
Example Row: x135.5 14.56 999999999 John Richard
Doe
On each line, the first character is not used (so read it into a
junk variable). In the example, the first character is
x
The second character indicates which type of employee the
individual is:
1. part-time hourly,
2. part-time salary,
3. full-time hourly without overtime,
4. full-time-hourly with double pay overtime,
5. full-time salary
In this example, the second character is 1
Immediately after the second character, is how many hours (float)
the employee
worked that week. In this example, the amount of hours
worked is 35.5.
Then white-space, and then either their annual salary
(double) or their
hourly rate (double). In this example, the hourly rate
is 14.56
Then white-space.
Next is their employee id number (long). In this
example, the employee id number is 999999999.
Then white-space.
Next is their first name, white-space, middle name, white-space,
last name. In this example it is John Richard
Doe
The program will output a " * " next to specific
employee IDs. When the document refers to a star, that is what it
is referring to.
All the modifications that must be made to my code are
in RED in the document below.
I Always Give Positive Feedback Language Is C Please Make Sure You Add Clear Explanations In The Code Of What Each P 1
I Always Give Positive Feedback Language Is C Please Make Sure You Add Clear Explanations In The Code Of What Each P 1 (156 KiB) Viewed 38 times
Homework 9 and 10 -- Reference Parameters in Functions Assignment Overview This assignment is an extension of Homework8, so important changes are in red like this... To summarize the assignment, you are using the first character on each input line to determine a boms for each employee. You have to modify each function to use reference parameters to 'pass back' the pay, the bonus, and the star in the parameters, and you must make the return value of each function to be return 0; (like maino ), which means the function finished OK Finally, your submitted code must also include 2 specific functions to calculate the bonus and the star "determination" called calcBonus(...) and calcStar(...), respectively For this assignment you are reading formatted input and writing formatted output. You read data one piece of data at a time (not whole lines) from an input file (whose name you will prompt for like you have used previously) until end-of-file. Each line of input is for 1 employee. You must use the extraction operator to read the data. You print a line of output for each employee in the format shown below and print summary totals at the bottom. You are not permitted to use arrays/vectors/etc. For each of 5 employee types, you need a function to calculate the weekly pay for each employee in the input file. You need to write and call the 5 functions listed below. The exact details of the functions are provided, and it is critical to match the details, e.g. the parameters and their data types, the return type(s), the exact output format. You must submit this assignment twice: to Homework and to Homework10. This assignment will also be graded by hand as Homework10 (like Homework4 was graded) based on Style, Format, and Documentation. The grading will be more rigorous, deducting more credit, if you make the same mistakes as you did on the first hand grading. You should have received feedback on mistakes on Homework4 and had few points deducted, but you should not make similar mistakes. Hopefully you leam with feedback. A grading rubric for Homework10 will be posted on Piazza. Problem Statement You are reading lines of data that are records of employee's data in the format of this: b345.5 14.56 999999999 Kenneth Todd Stevens On each line, the first character indicates what type of overtime bonus an employee is eligible for Letter 'a' - means the employee gets an hourly bonus of $0.10 per overtime-hour times their employee type (1*0.10 per hour for part-time hourly, 2*0,10 for part-time salary, 3*0.10 for full-time hourly w/o overtime, etc.) Letter 'b' - means the employee gets an hourly bonus of $0.20 per overtime-hour times their employee type (1*0 20 per hour for part-time hourly, 2*0,20 for part-time salary, 3*0.20 for full-time hourly w/o overtime, etc.)

. . Letter 'd' - means the employee gets an hourly bonus of $0.30 per overtime-hour times their employee (1*0.30 per hour for part-time hourly, 2*0.30 for part-time salary 3*0.30 for full-time hourly w/o overtime, etc.) Letter 'd' - means the employee gets an hourly bonus of $0.40 per overtime-hour times their employee type 1*0.40 per hour for part-time hourly, 2*0,40 for part-time salary, 3*0.40 for full-time hourly w/o overtime, etc.) Letter 'X' means no bonus, i.e. $0.00 Any other letter could be an error, but I will not test for this, you are guaranteed that the letter will be a-d or X The employee type is also used in calculating the bonus, because over 39.9 hours is overtime for part-time and over 40 for full-time employee. • For example, the example data line above contains "b345.5 14.56 ..." so this employee would get a bonus of o empType3) * (bonus level b) * (45.5 - 40) => 3* $0.20 * 5.5 The second character indicates which type of employee the individual is: 1. part-time hourly, 2. part-time salary, 3. full-time hourly without overtime, 4. full-time-hourly with double pay overtime 5. full-time salary Use a if statements (or switch) on this character, and have a case for each type in which you will call the respective calc function to return the employee's weekly pay. Immediately after each employee-type character (1-5) is how many hours (float) the employee worked that week, then white-space, and then either their annual salary (double) or their hourly rate (double). Each field after the hours-worked is white-space separated. Next are their employee id number (long), first name, middle namg last name Input can also look like this if you declare the variables to be of the correct type and use the extraction operator to read directly into those variables: b 45.5 14.56 999999999 Kenneth Stevens Functions Write the following separate functions to calculate each employee's pay for the week. You need additional parameters int calcPartTimeHourly(float hours, double hrate, char bonusType, double & pay, float & bonus, char &star) never paid for more than 39.9 hours. Mark with a *, if the employee works over 39.9 hours. int calcPartTime Salary(float hours, double sal, char bonusType, double & pay, float & bonus, char &star) .

• . paid 1/52 of annual salary. "Part time" concerns benefits. Mark with a * if the employee works under 35 hours int calcFull TimeHourly WithoutOvertime( float hours, double hrate, char bonusType, double & pay, float & bonus, char &star) • only paid "regular rate" for all hours, even hours over 40 hours int calcFullTimeHourly WithOvertime( float hours, double hrate, char bonusType, double & pay, float & bonus, char &star) paid double-time for over 40 hours. Mark with a *, if the employee works under 40 hours int calcFullTimeSalary(float hours, double sal, char bonusType, double & pay, float & bonus, char &star) paid 1/52 of annual salary. Mark with a * if the employee works under 40 hours. . . Each of these must return the calculated pay is a double. Each of the new versions of these functions must 1) retuin an int (indicating that the function ended normally, e.g. like main() retuins 0) and 2) have additional parameters used either to calculate the bonus or as return/reference parameters. The functions must be named exactly this and have exactly these parameter data types, eg, short, char, float, etc. FINALLY, you also must have 2 new functions with these exact signatures: float calcBonus (short employeeType, char bonusType, float hours) - return the dollar amount of the bonus based on the calculations described in the Problem Statement char calcStar (short employeeType, float hours) - returns either a '*" or a"' that is printed in the output You should use call these functions in each of the above calc... functions to calculate & return the bonus (as a float) and the star value (either a '*' or a'). In Web-CAT tests, I will call these functions directly, so that if you do not have these functions declared exactly like these, your code will not compile on Web-CAT Testing Like the previous assignment, you should test your own code before you start submitting it to Web-CAT. For each of the 10 or 12 different types of input cases you came up with for Homework8, you need to have a test case for each of the 5 types of bonus. This means you should have 50 or 60 different input lines for testing! For example, for the first employee type (calcPartTimeHowly), you need a line of input for an employee who works less than or equal to 39.9 hours and another line of input for another employee who works greater than 39 hours. You need 1. an input line for <39.9 hours with bonus-a, 2. an input line for <39.9 hours with bonus-b, 3. an input line for <39.9 hours with bonus-c, 4. an input line for <39.9 hours with bonus-d,

5. an input line for <39.9 hours with bonus-x, 6. an input line for > 39.9 hours with bonusa, 7. an input line for > 39.9 hours with bonusab, 8. an input line for >39.9 hours with bonusc, 9. an input line for > 39.9 hours with bonused, 10. an input line for >39.9 hours with bonus=x, The above list of input types is just for the first employee type! You need to figure this out for each of the types of input! Sample Input File a 240 10000.00 c3999 59.99 d1100.1 200 c541 9999.99 b499.9 100.01 329238390 Matthew Charles Mullenweg 327237237 Sergey Mikhailovich Brin 100000000 Edsger Wybe Dijkstra 123456890 Guido van Rossum 777777777 Kenneth Lane Thompson 11 Sample Output (corresponding to the above input) fixed and setprecision (2) is set. The id is 9 characters, then "+ "or" is printed; the name field is 15 characters, and the next 2 columns are first initial and middle initial with just 1 space between them. I space after the middle initial. Then base pay is printed in a field of 10 characters, left justified; bonus is printed in a field of 5, right justified, then $" and finally the sum of pay + bonus is printed. The cout format is given below. Hint: This is the output line format. cout << id << star << " « setw(15) « left « lname << fname[0] << " " << mname[0] << "" « setprecision (2) « left « setw(10) << pay « right << setw(5) << bonus << " $" << bonus + pay << endl; Name of input file: empin.txt ID NAME BASE BONUS 329238390 Mullenweg MC 192.31 0.02 327237237 Brin SM 59930.01 863.10 100000000* Dijkstra E W 7980.00 24.08 123456890 Rossum G v 192.31 1.50 777777777 Thompson KL 15981.60 47.92 Number of Employees: 5 Number of Hours Worked: 1280.00 Total Base Payroll: $84276.22 Total Bonus: $936.62 Total Payroll: $85212.84 Hint: You can access just the first character of a string with fname[0]; TOTAL PAY $192.33 $60793.11 $8004.08 $193.81 $16029.52 Another example input and output to show what happens when fields get bigger than their intended width. Another Input:

a 240 c3999 x449.9 b499.9 x4999.9 d599.9 10000.00 329238390 Matthew Charles NormalFields 59.99 327237237 Sergey Mikhailovich BigBonus 10.01 777777777 Don Jon ExampleOfWhatAReally LongNameDoes 100.01 777777777 Robert Xaviar NormalFields 9999.99 777777777 Robert Xaviar BigBasePay 40000.50 777777777 Robert Xaviar BigBonus $598.60 Another Output: Name of input file: empin.txt ID NAME BASE BONUS TOTAL PAY 329238390 NormalFields MC 192.31 0.02 $192.33 327237237 BigBonus SM 59930.01 863.10 $60793.11 777777777 ExampleOfWhatAReallyLongNameDoes D J 598.60 0.00 777777777 NormalFields RX 15981.60 47.92 $16029.52 777777777 BigBasePay RX 19597980.89 0.00 $195 97980.89 777777777 BigBonus RX769.24 119.80 $889.04 Number of Employees: 6 Number of Hours Worked: 2288.60 Total Base Payroll: $19675452.64 Total Bonus: $1030.84 Total Payroll: $19676483.48 Hint: This is the output line format. cout << id << star << « setw(15) « left « lname « fname[0] << "" « mname [O] «"" << setprecision (2) << left << setw(10) << pay << right « setw(5) << bonus << " $" << bonus + pay << endl;
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply