- #1
burns12
- 14
- 0
Homework Statement
I have to design a program that will calculate the hours worked by an employee in one day. Then calculate how much money they made that day, including overtime. You enter the times and the pay rate. Overtime pay is 1.5 normal.
For example
start: 8:50 A
end: 11:10 P
pay rate: 7.25
Employee has worked 14.33 hours
6.33 overtime hours
the paycheck is for $126.87
Homework Equations
The Attempt at a Solution
Code:
#include <iostream>
#include <string>
using namespace std;
float calculate_time(float hours, float minutes, float endhours, float endminutes);
float figure_pay(float hours, float payrate);
bool validate_times(float hours,float minutes,char apstart, char apend);
const int STANDARD_HOURS = 8;
int main()
{
string name;
bool valid;
char apstart, apend;
float hours, minutes, endhours, endminutes;
float payrate, totalhours, totalpay;
cout << "Time Clock Program" << endl << endl;
cout << "Enter worker name: ";
getline(cin, name);
cout << "Enter start time (hh:mm A/P): ";
cin >> hours >> minutes >> apstart;
cout << "Enter stop time (hh:mm A/P): ";
cin >> endhours >> endminutes >> apend;
cout << "Enter pay rate: ";
cin >> payrate;
valid = validate_times(hours, minutes, apstart, apend);
totalhours = calculate_time(hours, minutes, endhours, endminutes);
totalpay = figure_pay(hours, payrate);
cout << "The employee " << name << " has worked " << totalhours << " hours" << endl;
cout << "The total paycheck is for $" << totalpay;
system("pause");
return 0;
}
float calculate_time(float hours, float minutes, float endhours, float endminutes)
{
float totaltime, totalhours, totalminutes;
string name;
if(hours < 12 && endhours < 12)
{
totalhours = endhours - hours;
totalminutes = (endminutes/60) - (minutes/60);
totaltime = totalhours + totalminutes;
return totaltime;
}
if(hours < 12 && endhours > 12)
{
totalhours = (12 - hours) + endhours;
totalminutes = (endminutes/60) - (minutes/60);
totaltime = totalhours + totalminutes;
return totaltime;
}
/*if(totalhours - totalminutes < 0)
{
return 0;
}*/
return 0;
}
float figure_pay(float hours, float payrate)
{
float overtimepay, overtimehours, regpay, totalpay;
float overtimebonus = 1.5;
if(hours > STANDARD_HOURS)
{
overtimehours = hours - STANDARD_HOURS;
overtimepay = overtimehours*payrate*overtimebonus;
hours = 8;
regpay = hours*payrate;
totalpay = regpay + overtimepay;
return totalpay;
}
else
{
totalpay = hours*payrate;
return totalpay;
}
}
bool validate_times(float validhours,float validminutes,char apstart,char apend)
{
if(validhours > 12)
{
return false;
}
if(apstart == 'P' && apend == 'A')
{
return false;
}
if(validminutes > 59)
{
return false;
}
return true;
}
I cannot figure out how to add the times together. Or even how to read them in. I need the times to be numbers, but I have no idea how to do that since there has to be the colon in there. And the A or P for am or pm. Any guidance here would be great.
Thanks