How can I design a program to calculate employee hours and pay?

AI Thread Summary
The discussion focuses on designing a program to calculate employee hours and pay, including overtime calculations. Key elements include parsing time input in AM/PM format, converting it to a 24-hour format, and calculating total hours worked and pay based on a specified pay rate. Participants suggest using functions like strtok and atoi for string manipulation and conversion, while emphasizing the need to handle input validation and time calculations correctly. The conversation highlights the challenges faced by a programmer with limited experience in C++, particularly regarding time input and arithmetic operations. Overall, the thread provides guidance on structuring the program and overcoming common programming hurdles.
burns12
Messages
13
Reaction score
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
 
Physics news on Phys.org
You're going to have to parse the input time strings to separate the hour and minute section and determine whether the time is AM or PM. One possiblity is to use the C standard library strtok function. You call it with the string to search (your input string containing the time) and a separator (such as ':') and it returns a pointer to the first token, a substring containing the hour. After finding a string containing the hour, you need to convert the string of characters to a number, using the atoi function.

If you called strtok again, this time with a separator of ' ' (a space), it would return the second token, a substring containing the minutes. This would assume that the user separates the time from the A or P designator with a space.

After pulling the hour and minutes values from a string and converting each to an int, you could convert to 24 hour time, which would make it easier to calculate the elapsed time. For example, if someone started work at 8:15 A and quit work at 5:30 P, the starting time would be 815 and the ending time would be 1730, and the elapsed time would be 915, or 9 hours and 15 minutes. Alternatively you could write the times in decimal form, with the starting time being 8.25 and ending time 17.5, for an elapsed time of 9.25 hours, which is the same as 9 hours and 15 minutes.

Finally, to search for A or P, you could use strchr, which returns a pointer to the location in the string where the character is found, or null if not found. Your logic could be to search for an 'A' character. If found, the time is AM and you're done. If not found, search for a 'P' character. If found, the time is PM. If not found, the input string is malformed.

strtok and strchr are declared in string.h. atoi is declared in stdlib.h.

Hope this helps.
 
That does help actually, it makes sense to do it like that now that I see it. But the problem is we haven't done any of that in class. I have some programming experience in java and this is c++ so I know the basics but this is killing me. We haven't done loops yet even. All we have to work with is functions and if/else statements.

How do you get it to convert the time to 24 hour time?
What type should I make the input variables? Ints or strings or what?
 
burns12 said:
That does help actually, it makes sense to do it like that now that I see it. But the problem is we haven't done any of that in class. I have some programming experience in java and this is c++ so I know the basics but this is killing me. We haven't done loops yet even. All we have to work with is functions and if/else statements.
For what you need to do, you don't need loops. You are taking a starting time and a quitting time and calculating the number of hours and the pay - that's all.
burns12 said:
How do you get it to convert the time to 24 hour time?
You need to do it in two parts: the hour and the minutes.
If the hour is earlier than 1 PM, multiply by 100. For example 8 AM is 800.
If the hour is 1 PM or later, add 2. For example, 1:00 PM is 1300.

After you have converted the hours to 24-hour time, add on the minutes. For example: 8:30 AM becomes 830 and 2:15 PM becomes 1415.

This leads to some difficulties, though, because if someone starts work at 8:30 (830) and works to 2:15 (1415), you might naively calculate the elapsed time as 1415 - 830 = 585, which is incorrect. The actual amount of time is 5 hours and 45 minutes.

To handle this difficulty it might be simpler to convert to hours + decimal part. Using the same times,
8:30 = 8.5
2:15 = 14.25

14.25 - 8.5 = 5.75, which is 5 hours and 45 minutes, the correct elapsed time.

To summarize, after parsing the input string into hour and minutes, convert any times after 1 pm by adding 2, and convert minutes to fractions of hours by dividing the minutes by 60.

For example 4:55 PM = 16.916667 (rounded).

burns12 said:
What type should I make the input variables? Ints or strings or what?
Making the input times strings would make for simpler user input, but more complicated code, since you have to take the string apart and extract the information in it.
 
Ok I get the 24 hour time thing, I think I have that function corrected now. Now about taking this string apart, can I use substrings? I tried that and can get everything extracted how I need it, but how do I get them to floats so the function will accept them?

I can't type cast them can I?

No, I can't, nevermind that
 
Suppose you have the hour portion and the minutes in two int variables named hr and min. You can get the time as a float by doing this:

startTime = hr + min/60.0;

This assumes that hr is already in the right form (800 for 8:00, and 1400 for 2:00 PM).
 
I just cannot see how to get these string values into a number I can do arithmetic with. I've tried everything I can think of, there is no way to turn a string into an int that I can think of without using libraries we haven't learned of yet.
 
The atoi and atof functions in stdlib.h can be used to convert strings of characters to, respectively, int and double values.

If you don't want to use the old-style C standard library, the string class has some methods that can be used to get a substring out of a string object (the substr method). Once you have a string of digits, you can convert it to a decimal number by knowing how the ASCII codes for the characters '0', '1', ..., '8', and '9' map to the digits 0, 1, 2, ..., 8, and 9.

At this point I think that it would be a good idea for you to ask your instructor how he expects you to write this program. Tell him/her that you aren't looking for the code itself, but some guidance about the direction to go. There are a number of C standard library functions that could be used, or you could use the members available in the string class.
 

Similar threads

Replies
3
Views
1K
Replies
6
Views
4K
Replies
6
Views
3K
Replies
19
Views
3K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
17
Views
5K
Replies
17
Views
3K
Back
Top