0

I need help in pulling this program apart, and reformatting it to use functions that I didn't know i needed to implement until right now.

* float calculate_time (4 parameters) finds the amount of hours between two times given. It should return 0 if the amount is less than 0.
* float figure_pay (2 parameters) returns the amount of actual pay the employee gets, including overtime if applicable. This function will access a global named constant.
* bool validate_times (4 parameters) returns true if the start time is before the stop time, false otherwise

Any hints to the right direction would be greatly appreciated.

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	string name;
	string fileName;
	
    const char testA = 'A';
    const char testP = 'P';
    float startHr;
	float startMin;
	float stopHr;
	float stopMin;
	char colon;
	char time1;
	char time2;
	float rate;
	float startMinPercent ;
	float stopMinPercent;
	float startPercent;
	float stopPercent;
	float totalTime;
	float pay;
	float overTime;
	ifstream inData;

	cout << "Time Clock Program" << '\n';
	cout << "Enter the filename of the data to process --> ";
	cin >> fileName;
	cout << '\n';

	fileName = fileName + ".dat";

	inData.open(fileName);

	if (inData.is_open())
	{

		getline(inData, name);
		cout << "Name: " << name;
		cout << '\n';


		inData >> startHr >> colon >> startMin >> time1;
		cout << "Start time " << startHr << colon << setfill('0') << setw(2) << startMin << " " << time1;
		cout << '\n';

		inData >> stopHr >> colon >> stopMin >> time2;
		cout << "Stop time " << stopHr << colon << setfill('0') << setw(2) << stopMin << " " << time2;
		cout << '\n';

		inData >> rate;
		cout << "Pay rate $" << setiosflags(ios::fixed) << setprecision(2) << rate << " per hour";
		cout << '\n' << '\n';

		time1 = (char)toupper(time1);
		time2 = (char)toupper(time2);

		if ( startHr > 12 || startHr <= 0 )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( startMin > 59 || startMin < 0 )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( time1 != 'P' && time1 != 'A' )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( stopHr > 12 || stopHr < 0 )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( stopMin > 59 || stopMin < 0 )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( time2 != 'P' && time2 != 'A' ) 
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( time1 == 'P' && time2 == 'A' )
		{
			cout << "Invalid time! Time set to 0:00" << '\n';

			cout << "Invalid time! Time set to 0:00" << '\n';

			startHr = 0;
			startMin = 00;
			stopHr = 0;
			stopMin = 00;
		}
		else if ( time1 == 'A' && time2 == 'A' )
		{
			if ( startHr >= stopHr )
			{
				if ( startMin >= stopMin )
				{
					cout << "Invalid time! Time set to 0:00" << '\n';

					cout << "Invalid time! Time set to 0:00" << '\n';

					startHr = 0;
					startMin = 00;
					stopHr = 0;
					stopMin = 00;
				}
				else;
			}
			else;
		}

		if ( time1 == 'A' && time2 == 'A' )
		{
			startMinPercent = startMin/60;
			stopMinPercent = stopMin/60;

			startPercent = startHr + startMinPercent;
			stopPercent = stopHr + stopMinPercent;

			totalTime = stopPercent - startPercent;

			if( totalTime > 8 )
			{
				overTime = totalTime - 8;

				pay = (overTime * (rate * 1.5)) + ( 8 * rate);

				cout << "The employee " << name << " has worked " << setiosflags(ios::fixed) << setprecision(2) << totalTime << " hours" << '\n';
				cout << overTime << " hours overtime" << '\n';
				cout << "The paycheck is for $" << setiosflags(ios::fixed) << setprecision(2) << pay << '\n';
			}
			else
			{
				pay = totalTime * rate;

				cout << "The employee " << name << " has worked " << setiosflags(ios::fixed) << setprecision(2)<< totalTime << " hours" << '\n';
				cout << "No overtime" << '\n';
				cout << "The paycheck is for $" << setiosflags(ios::fixed) << setprecision(2) << pay << '\n';
			}
		}
		else
		{
			startMinPercent = startMin/60;
			stopMinPercent = stopMin/60;

			startPercent = startHr + startMinPercent;
			stopPercent = stopHr + stopMinPercent;

			totalTime = (12 + stopPercent) + (12 - startPercent);

			if ( time1 == 'P' && time2 == 'P' )
			{
				overTime = totalTime - 8;

				pay = (overTime * (rate * 1.5)) + ( 8 * rate);

				cout << "The employee " << name << " has worked " << setiosflags(ios::fixed) << setprecision(2) << totalTime << " hours" << '\n';
				cout << overTime << " hours overtime" << '\n';
				cout << "The paycheck is for $" << setiosflags(ios::fixed) << setprecision(2) << pay << '\n';
			}
			else
			{

				totalTime = stopPercent + (12 - startPercent);

				if(totalTime > 8 )
				{
					overTime = totalTime - 8;

					pay = (overTime * (rate * 1.5)) + ( 8 * rate);

					cout << "The employee " << name << " has worked " << setiosflags(ios::fixed) << setprecision(2) << totalTime << " hours" << '\n';
					cout << overTime << " hours overtime" << '\n';
					cout << "The paycheck is for $" << setiosflags(ios::fixed) << setprecision(2) << pay << '\n';
				}
				else
				{
					totalTime = stopPercent + (12 - startPercent);

					pay = totalTime * rate;

					cout << "The employee " << name << " has worked " << setiosflags(ios::fixed) << setprecision(2) << totalTime << " hours" << '\n';
					cout << "No overtime" << '\n';
					cout << "The paycheck is for $" << setiosflags(ios::fixed) << setprecision(2) << pay << '\n';
				}
			}
		}
	}
	else
	{
		cout << "Sorry that file does not exist." << '\n';
		cout << "Program closing" << '\n' << '\n';
	}

	system ("pause"); 
	return 0;
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by jonsca
0

Rather than initially trying to shuffle your code all around, make a new file with a new main.

Write the functions in that new file and test them with simple cases using the new main.

Then once you've got them tested, copy them back into the old main and prototype them. Find out where you need those calculations, and substitute the function calls for the block of code you were using to do the same task before.

I know that's probably not as specific as you wanted it to be, but give it a try and post back.

Just an aside, if all of your else ifs from 65-140 are doing the same work, why not combine them into one line. I think as long as you make it readable, it should be more effective, and I think the reader will understand your goals there.

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.