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

In summary: Take the starting time and calculate the number of hours and minutes as above. Call these startTimeHours and startTimeMinutes.2. Take the quitting time and calculate the number of hours and minutes as above. Call these endTimeHours and endTimeMinutes.3. Subtract the starting time hours from the quitting time hours. If the result is negative, subtract 1 from the quitting time hours and add 60 to the quitting time minutes. For example, calculating the elapsed time between 1:00 PM and 8:00 AM would give you 8 - 1 = 7 hours and 0 - 60 = -60 minutes, which you would then add on to give 6 hours.4. Subtract the starting time minutes from
  • #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
 
Physics news on Phys.org
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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
 
  • #6
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).
 
  • #7
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.
 
  • #8
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.
 

1. What is a "Comp sci program about time"?

A "Comp sci program about time" is a computer science program that focuses on the study and application of time-related concepts in computing, such as algorithms for time management, time complexity analysis, and real-time systems.

2. What are the benefits of studying a "Comp sci program about time"?

Studying a "Comp sci program about time" can provide you with the skills and knowledge to develop efficient and accurate time-based algorithms, as well as the ability to design and implement real-time systems. These skills are highly sought after in industries such as finance, telecommunications, and transportation.

3. What courses are typically included in a "Comp sci program about time"?

Courses in a "Comp sci program about time" may include data structures and algorithms, real-time systems, computer architecture, time complexity analysis, and operating systems. Other elective courses may also be offered, such as artificial intelligence, data mining, and machine learning.

4. What career opportunities are available for graduates of a "Comp sci program about time"?

Graduates of a "Comp sci program about time" can pursue careers as software engineers, systems analysts, real-time software developers, and data scientists, among others. They can also work in a variety of industries, including finance, healthcare, transportation, and telecommunications.

5. What skills are necessary for success in a "Comp sci program about time"?

To be successful in a "Comp sci program about time," you should have a strong foundation in computer science principles, programming languages, and data structures. Additionally, strong mathematical and analytical skills are necessary for understanding and developing time-based algorithms.

Back
Top