> How do I find the gross pay for each employee using for loop in C++?

How do I find the gross pay for each employee using for loop in C++?

Posted at: 2014-12-18 
Use this code as example: http://algoritmosurgentes.com/en/cpp.php...

The variables 'hours' and 'hourlyR' are constantly getting overwritten, so by the end of it, you only have employee 5's hours and hourlyR, which is why output of their gross pay is the same for each employee.

Can you use structs? That would prove to be much easier and eliminate the tediousness involved with making tons of variables for each employee.

I am in a C++ class at my college. I got how the gross pay is calculated, but I don't know how to calculate for each employee. I want to use for loop to calculate the gross pay for 5 employees. This is my code:

#include

using namespace std;

int main()

{

//Assign variables

int hours;

double grossPay, hourlyR;

//Aks each employee to input the amout of hourly rate.

cout << "Enter your hours and hourly rate, Employee 1.\n";

cin >> hours;

cin >> hourlyR;

cout << "Enter your hours and hourly rate, Employee 2.\n";

cin >> hours;

cin >> hourlyR;

cout << "Enter your hours and hourly rate, Employee 3.\n";

cin >> hours;

cin >> hourlyR;

cout << "Enter your hours and hourly rate, Employee 4.\n";

cin >> hours;

cin >> hourlyR;

cout << "Enter your hours and hourly rate, Employee 5.\n";

cin >> hours;

cin >> hourlyR;

//Use the for loop with if statement to calculate the gross pay for 5

//employees who execeeds hourly rate of 40.

for (int i = 1; i <= 5; i++){

if (hours > 40)

grossPay = (hourlyR * 40) + 1.5 * (hourlyR) * (hours - 40);

else

grossPay = hourlyR * hours;

}

//Output the grossPay for the 5 employee

cout << "Employee 1's gross pay is " << grossPay << endl;

cout << "Employee 2's gross pay is " << grossPay << endl;

cout << "Employee 3's gross pay is " << grossPay << endl;

cout << "Employee 4's gross pay is " << grossPay << endl;

cout << "Employee 5's gross pay is " << grossPay << endl;

return 0;

}