i know my if statement is not correct somewhere but i do not know where? or maybe its just the logical error?
After 8 hours any hours worked will be paid time and a half. That is, given the wage per hour multiply by 1.5. That wage is paid for hours after 8 hours.
After 10 hours any hours worked will be paid double time. That is, given the wage per hour multiply by 2.0. That wage is paid for hours after 10 hours.
Please show: ( Example output )
Wage per hour: 12.37
Hours worked : 10.3
Pay for ( 0 to 8 hours) : 98.96
Pay for hours 8 to 10) : 37.11
Pay for hours (10 and beyond): 7.42
Total Gross Pay : 143.49
// Example program
#include <iostream>
#include <string>
using namespace std;
double HoursWorked;
double WagePerHour;
double TotalWages;
double TimeAndHalf;
double Overtime;
char ContinueChar;
//test cases: 10.5 hours # 12/hour = $96.00, 2 hours at 1.5 rate = $36.00, .5 hour at 2.0 rate = $12.00
// 6.3 hours # 12/hour = 75.6, no hours of overtime or double time
//12.5 hours # 14.34/ hour = $114.72, 2 hours at 1.5 rate = 43.02, 2.5 hours at 2.0 rate = $71.70
//3.7 hours # 19/hour = $70.30
// 14 hours # 23.50/hour = $188, 2 hours at 1.5 rate = $70.50, 4 hours at 2.0 rate = $188
//I tested with test test cases and the program had the same results.
int main()
{
cout << "Ticket #64220\n";
cout << "CMPR-120\n";
cout << "Instructor : Joel Kirscher\n";
cout << "Student: Seyed Shariat\n";
cout << "Payroll Overtime";
cout << "\n\n";
do {
cout << "How many hours did you work this pay period: \n";
cin >> HoursWorked;
cout << "What wage do you get paid per hour?: \n";
cin >> WagePerHour;
cout << "So you your paycheck will be: " << HoursWorked * WagePerHour << " before taxes are taken out. \n";
if (HoursWorked > 8 && <= 10)
cout << "For the hours you worked over 8, and less than or equal to 10 you made: " << HoursWorked * 1.5 * WagePerHour << " \n";
else (HoursWorked >10);
cout << "For the hours you worked over 10: " << HoursWorked * 2.0 * WagePerHour << " \n";
cout << "Do you want this to run again? (y=Yes): ";
cin >> ContinueChar;
} while (ContinueChar == 'y' || ContinueChar == 'Y');
cin.get();
return 0;
}
There are many approaches to this. Here is one. The comments may help you to follow my reasoning. Fortunately the mathematics behind this is not very complicated.
/*
We need to find the area under this pay rate / hours worked graph.
The following approach divides the graph into three rectangles as shown below
2.0 | +--+
| | |
1.5 | +-+--|
| | |
1.0 +-------+----|
| |
0.5 | |
| |
0.0 +------------+
0 8 10
*/
// Calculate basic pay before any overtime consideration
Pay = HoursWorked * HourlyRate;
// Hours above 8 earn half-again more than the standard wage.
// (This will be only positive if more than 8 hours were worked.)
OvertimePay = (HoursWorked - 8.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;
// Hours above 10 earn an additional 50% extra
// (This will be only positive if more than 10 hours were worked.)
OvertimePay = (HoursWorked - 10.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;
Related
I'm having some trouble with this, and just wondering if anyone can help. I'm new to all this, and getting the correct value for minutes is really bothering me. If I were to put in 1850 as a departure time, and 1900 as the arrival time, it should be ten minutes, but instead it's 50 due to it being an integer. Any advice?
Question:
Write a program that will ask the user to enter the departure time and arrival time for a train journey. The times should be entered as per a 24-hour clock, for example quarter to seven in the evening is 1845. Assuming that the journey is within the same 24-hour period, the program should calculate and display the duration of the journey in hours and minutes.
#include <iostream>
using namespace std;
int main()
{
int diff, mins, hours, arrTime, depTime;
cout << "\nDeparture: ";
cin >> depTime;
cout << "\nArrival: ";
cin >> arrTime;
diff = arrTime - depTime;
hours = diff / 100;
mins = diff % 100;
if(mins > 60){
hours++;
mins -= 60;
}
cout << "Hours: " << hours << " Minutes: " << Minutes;
return 0;
}
I'm trying to obtain the data for the last employee Bob Hu. But for some reason, it doesn’t want to show up. Does anyone know what I'm doing wrong.? I try to put the calculation/display code into a function() and call it where you currently calculate/display the data and also after while() terminates. But I think I'm doing it wrong. Please help
This is my code.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void find_pay(string x)
{
ifstream inputFile;
string filename;
// Get the filename from the user.
cout << "Enter the filename: ";
cin >> filename;
// Open the file.
inputFile.open(filename);
// If the file successfully opened, process it.
if(inputFile.is_open())
{
string data_record,prev,data_name,data_hour;
double total = 0;
while(getline(inputFile,data_record))
{
data_name = data_record.substr(0,30);
data_hour = data_record.substr(31,2);
if(prev.compare(data_name)!= 0 )
{
//Calculate the gross pay, tax, net pay
float gross_pay = 18 * total;
float tax = (12 * gross_pay)/100;
float net_pay = gross_pay - tax;
if(total!= 0)
cout << "Employee:" << prev << "\n" "Total Hours: " << total << " Gross Pay: $ " << gross_pay << " Tax " << tax << " Net Pay: " << net_pay << endl;
data_name = data_record.substr(0,30);
prev = data_name;
total = 0;
}
else if(prev.compare(data_name)==0)
{
total += stod(data_hour);
}
}
//close the file
inputFile.close();
}
}
int main()
{
string filename;
find_pay(filename); //call the void
return 0;
}
This is the output that I got, as you see Bob Hu is missing
Enter the filename: /Users/normatacuri/Desktop/employeehours.txt
Employee:Jimmy Bucket
Total Hours: 26 Gross Pay: $ 468 Tax 56.16 Net Pay: 411.84
Employee:John Doe
Total Hours: 32 Gross Pay: $ 576 Tax 69.12 Net Pay: 506.88
Employee:Ann Doe
Total Hours: 20 Gross Pay: $ 360 Tax 43.2 Net Pay: 316.8
Employee:Mary Jones
Total Hours: 16 Gross Pay: $ 288 Tax 34.56 Net Pay: 253.44
Program ended with exit code: 0
This is the data on the file that I'm using..
Jimmy Bucket 8
Jimmy Bucket 9
Jimmy Bucket 10
Jimmy Bucket 7
John Doe 8
John Doe 8
John Doe 8
John Doe 8
John Doe 8
Ann Doe 5
Ann Doe 5
Ann Doe 5
Ann Doe 5
Ann Doe 5
Mary Jones 4
Mary Jones 4
Mary Jones 4
Mary Jones 4
Mary Jones 4
Bob Hu 8
Bob Hu 8
Bob Hu 8
Bob Hu 8
Bob Hu 8
I try to put the calculation/display code into a function() and call it where you currently calculate/display the data and also after while() terminates. But I think I'm doing it wrong.
You are right, you must have done it wrong, but your approach of handling the case also after while() terminates was good. Since you don't show this try, I can't say what you were doing wrong. But I can show you an alternative approach where the EOF case is also handled inside the loop - for this replacement of the while loop only a few statements have to be rearranged.
for (; ; )
{ bool f = (bool)getline(inputFile, data_record);
if (!f || prev.compare(data_name = data_record.substr(0, 30)) != 0)
{ // do this on EOF as well as new name
//Calculate the gross pay, tax, net pay
float gross_pay = 18 * total;
float tax = (12 * gross_pay)/100;
float net_pay = gross_pay - tax;
if (total != 0)
cout << "Employee:" << prev << "\n"
"Total Hours: " << total << " Gross Pay: $ " << gross_pay
<< " Tax " << tax << " Net Pay: " << net_pay << endl;
if (!f) break;
prev = data_name;
total = 0;
}
total += stod(data_hour = data_record.substr(31, 2));
}
If a person has low income (<= 25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.
For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.
(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 * 0.40 * (45 / 60) = $21.00.)
Write a program in C++ that prompts the user to enter the hourly rate, the total consulting time, and whether the person has low income. The program should output the billing amount.
This is what I have done so far and the program looks correct to me, but for some reason I do not get the correct output. I get 0.00 which is a wrong output value.
#include<iostream>
#include <iomanip>
using namespace std;
double calculateBill(int income, int consultingMinutes, double hourlyRate);
int main()
{
int income;
double consultingMinutes;
double hourlyRate;
cout << "Please enter the clients income: $" ;
cin >> income;
cout << "Please enter the consulting time in minutes: ";
cin >> consultingMinutes;
cout << "Please enter the hourly rate: $";
cin >> hourlyRate; cout << fixed << showpoint << setprecision(2);
cout << "Your total bill ammount comes to: $" << calculateBill(income, consultingMinutes, hourlyRate) << endl;
return 0;
}
double calculateBill(int income, int consultingMinutes, double hourlyRate)
{
if (income <= 25000) {
if (consultingMinutes <= 30)
return 0;
else
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
}
else {
if (consultingMinutes <= 20)
return 0;
else
return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);
}
}
Mixing of integers and floats is not a very good thing. Try this:
return hourlyRate * 0.40 * (((double) consultingMinutes - 30.0) / 60.0);
instead of this:
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
(And apply the same fix for the second wrong place return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);)
I'm writing this program that calculates the amount of hours that employees work. If the employee ends up working more than 40 hours, than he'll get an additional time and half for every hour that passes 40. My issue is when it comes to the output. In the output, it shows the amount of sales tax, federal tax, gross income, and net; however if the employee puts in that he has worked less than 40 hours, everything displays properly; however, once the employee puts in that he has worked more than 40 hours for some reason it shows the taxes, and net, gross as 0.00.
For example, if less than 40, the following would display.
F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross:
Employee Last 39.00 22.00 1.50 60.06 128.70 34.32 858.00
If more than 40, the following is being displayed.
F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross:
Employee Last 45.00 22.00 1.50 0.00 0.00 0.00 0.00
Any tips, hints, advice is appreciated.``
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Set variables
int const size = 2;
double timeHalf = 0.0, hoursWorked[size], gross[size], net[size], hourlyRate[size], totalTax;
string firstName[size], lastName[size];
const double stateTax = 0.07, fedTax = 0.15, fica = 0.04;
double sTax = 0.0, fTax = 0.0, fiTax;
for(int i = 0; i < size; i++) //Initialize for loop
{
cout<<"Please enter your first name? " <<endl; //Display first name
cin>>firstName[i];
cout<<"Please enter your last name? " <<endl; //Display last name
cin>>lastName[i];
cout<<"How many hours did you work? " <<endl; //Dispaly hours worked
cin>>hoursWorked[i];
cout<<"What's the hourly rate? " <<endl; //Display hourly rate
cin>>hourlyRate[i];
if(hoursWorked[i] < 40)
{
gross[i] = hoursWorked[i] * hourlyRate[i];
}
else if( hoursWorked[i] > 40)
{
gross[i] = hoursWorked[i] * (hourlyRate[i] * timeHalf);
}
}
cout<<" \t\tXYX \n ";
cout<<"F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross: Net: \n";
for(int i = 0; i < size; i++)
{
sTax = gross[i] * stateTax;
fTax = gross[i] * fedTax;
fiTax = gross[i] * fica;
totalTax = gross[i] * (stateTax + fedTax + fica);
timeHalf = 1.5;
net[i] = gross[i] - totalTax;
cout<<firstName[i]<<"\t " << setw(10) <<fixed <<setprecision(2) << lastName[i]<<"\t "
<< hoursWorked[i]<<"\t " <<hourlyRate[i] <<"\t " <<timeHalf<<"\t " <<sTax <<"\t "
<<fTax <<"\t " <<fiTax <<"\t " <<gross[i] <<"\t " <<net[i] <<"\t ";
}
}
Because in your conditions, you check if(hoursWorked[i] < 40) and if(hoursWorked[i] > 40), but there is no condition for if(hoursWorked[i] == 40). You probably wanted to use -or-equal operator on one of these conditions (>= or <=).
4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the
gross pay for each of several employees.
When someone works 41 hours or more. They get paid 1.5x more so my problem is that in my else if statement. I did rate * 1.5 which should translate into 10 * 1.5 = 15 but I get 425 because my code on the top probably, but I don't understand it at the moment.
He gave us this example. Which I'm trying to emulate.
"
Enter hours worked (-1 to end): 41
Enter hourly rate of the employee ($00.00): 10.00
Salary is $415.00
"
#include <iostream>
using namespace std;
int main()
{
double salary=0,rate=0,hours=0,counter=0;
cout << "Enter your hours worked: ";
cin >> hours;
cout << "Enter your rate: ";
cin >> rate;
while(counter<=hours)
{ salary=hours*rate;
counter++;
}
if(hours==-1)
{exit(0);
}
else if(hours>=41)
{
rate = rate * 1.5;
salary = salary + rate;
}
cout << "$" << salary;
return 0;
}
The loop you used has no functionality related to the problem, so I omitted it. Below is what will work. As others have said, define why you need a loop. I'm guessing you need to loop the code so the user can repeat this to their heart's content. If that is the case, I'll let you try to figure out how to break out of the loop when a user enters -1.
if (hours == -1)
exit(0);
else if (hours <= 40)
salary = rate * hours;
else {
double overtimeHours = hours - 40;
salary = rate * 40;
rate = rate * 1.5;
salary = salary + (rate * overtimeHours);
}
cout << "$" << salary;