functions and looping

This program should loop and ask employees for their ID, number of hours works and their hourly wage. it should keep collecting info on different employees until an ID # of -1 is entered at which point the program should neatly display all the employee ID's that were entered and each ones gross pay

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// File: Gross pay calculator using functions.cpp
// Programmer: Sadia Rajput CPSC 1103 S10
// Purpose: Computes a worker's gross pay

#include<iostream>
#include<iomanip>

using namespace std;

//*****************************************************
//                Function Prototypes
//*****************************************************

	void intro();											// Displays welcome message
	void getInput(int&, double&, double&);					// Gets user input
	double computeGrossWage(double, double);				// Calculates gross pay based on hours worked & hourly wage
	void displayEmptyData(int, double, double, double);		// Displays, employee ID #, hours worked, hourly pay, gross pay

//*****************************************************
//                   Main Program
//*****************************************************

int main()
{
	// Variables
		int employeeIDnumber;
		double hoursWorked;
		double hourlyWage;
		double grossPay;
	//Function calls
		intro();											// Calls function to display welcome message
		do
		{
		getInput(employeeIDnumber, hoursWorked, hourlyWage);// Calls function to get user input
		grossPay = computeGrossWage(hourlyWage, hoursWorked);
		computeGrossWage(hourlyWage, hoursWorked);			// Calls function to calculate gross pay
		displayEmptyData(employeeIDnumber, hoursWorked, hourlyWage, grossPay);
		} while (employeeIDnumber != -1);
		
	// Exit
		system("pause");
		return 0;
}

//*****************************************************
//                Function Definitons
//*****************************************************

void intro()
	// Purpose:    Displays welcome message 
	// Parameters: None
	// Returns:    None
		{
			cout << "Welcome to the gross pay calculator" << endl;
		}
void getInput(int& employeeIDnumber, double& hoursWorked, double& hourlyWage)
	// Purpose:    Gets user input 
	// Parameters: Employee ID number, hours worked, hourly wage
	// Returns:    None
		{
			do
			{
				cout << "Enter employee ID number: ";
				cin >> employeeIDnumber;

				cout << "Enter number of hours worked: ";
				cin >> hoursWorked;

				cout << "Enter hourly wage: ";
				cin >> hourlyWage;
			}while (employeeIDnumber != -1);
			cout << endl;
		}
double computeGrossWage(double hourlyWage, double hoursWorked)
	// Purpose:    Calculates gross pay based on hours worked & hourly wage 
	// Parameters: Hourly wage, hours worked
	// Returns:    Gross pay
		{
			double grossPay;	
			if (hoursWorked <= 40)
				grossPay = (hoursWorked * hourlyWage);
			else 
				grossPay = (hourlyWage * (hoursWorked - 40) * 1.5) + (40 * hourlyWage); 
			return grossPay;
		}
void displayEmptyData(int employeeIDnumber, double hoursWorked, double hourlyWage, double grossPay)
	// Purpose:    Displays results of gross pay calculation
	// Parameters: employee ID #, hours worked, hourly wage, gross pay
	// Returns:    None
		{
			cout << "Employee ID number: " << employeeIDnumber << endl;
			cout << "Gross Pay: $" <<fixed << setprecision(2) << grossPay << endl; 
		}
The way you used tabs makes it not look so good when I copypasted, but anyway, I tried some stuff out and it seems to work.

First off, your sentinel (-1) is in a bad place I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
			do
			{
				cout << "Enter employee ID number: ";
				cin >> employeeIDnumber;
				if (employeeIDnumber == -1)
				break;

				cout << "Enter number of hours worked: ";
				cin >> hoursWorked;

				cout << "Enter hourly wage: ";
				cin >> hourlyWage;
                
                values.push_back(employeeIDnumber);
                number_of_employees++;
			}
            while (employeeIDnumber != -1);
			cout << endl; 


Also, I added two variables to your list. The vector is pushed back to keep track of the number of times a new employee ID is added to the getInput cycle, as seen above and at the for loop, and the number_of_employees was created to give an end to the for loop I made at the end. I might've read somewhere that the way I used vectors is not the best way to do it, but it works gross pay still needs to be updated, see bottom edit.
1
2
3
4
5
6
7
	// Variables
		int employeeIDnumber;
		int number_of_employees = 0;
		double hoursWorked;
		double hourlyWage;
		double grossPay;
		vector<int> values;


1
2
3
4
5
6
7
8
9
10
11
12
void displayEmptyData(int employeeIDnumber, double hoursWorked, double hourlyWage, double grossPay, int number_of_employees, vector<int> values)
	// Purpose:    Displays results of gross pay calculation
	// Parameters: employee ID #, hours worked, hourly wage, gross pay
	// Returns:    None
{
	    for (int i = 0; i < number_of_employees; i++)
		{
		    
			cout << "Employee ID number: " << values[i] << endl;
			cout << "Gross Pay: $" <<fixed << setprecision(2) << grossPay << endl << endl; 
		}
}


Edit: I forgot that something similar needs to be done with the grosspay, otherwise it makes the Payment for all employees the same as the last employee entered. I haven't tried it out, but I think you could turn the pay calculation function into a void function and then make a vector that keeps track of the grosspay each time like the other vector.
Last edited on
Thank you, this helped a lot =)
Topic archived. No new replies allowed.