Need help with HW, payroll array using long integers C++

9. Payroll Write a program that uses the following arrays:
• empId : an array of seven long integers to hold employee identification
numbers. The array should be initialized with the following numbers:
5658845 4520125 7895122 8777541 8451277 1302850 7580489

• hours : an array of seven integers to hold the number of hours worked by
each employee
• payRate : an array of seven double s to hold each employee’s hourly pay
rate

• wages : an array of seven double s to hold each employee’s gross wages
The program should relate the data in each array through the subscripts.
For example, the number in element 0 of the hours array should be the
number of hours worked by

the employee whose identification number is stored in element 0 of the
empId array. That same employee’s pay rate should be stored in element 0 of the payRate array. The program should display each employee number and ask the user to enter that employee’s hours and pay rate. It should then calculate the gross wages for that employee (hours times pay rate) and store them in the wages array. After the data has been entered for all the employees, the program should display each employee’s identification number and gross wages. Input Validation: Do not accept negative values for hours or numbers less than 15.00 for pay rate.


I dont know why its asking to do everything over, even if i put in the correct info its something in the loop that's killing me please help



#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;


int main()
{

const int empId = 7; // constant for the array size
int workers[empId] = { 5658846, 4520125, 7895122, 8777541,
8451277, 1302850,7580489 };// Giving 7
int hours[empId];
double payRate[empId];

\
cout << "Please enter the hours worked by " << empId
<< " employees and their\n"
<< "hourly pay rates.\n";
for (int index = 0; index < empId; index++)
{
cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
cin >> hours[index];// user input
cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
cin >> payRate[index];

do
{
cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
cin >> hours[index];

if (hours[index] < 0)
{
cout << "Enter in a positive number" << endl;
}
} while (hours[index] < 0);

do
{
cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
cin >> payRate[index];

if (payRate[index] < 15.00)
{
cout << "The pay rate must be at least $15.00. " << endl;
}
} while (hours[index] < 6);

cout << "This is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
}
for (int index = 0; index < empId; index++)
{
double grossPay = hours[index] * payRate[index];
cout << "Employee #" << (index + 1);
cout << ": earned $" << grossPay << endl << endl;
}

system("pause");
return 0;

}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Can you be more specific? What is it that you are being asked to "do over"? Which loop is it that seems to be running more times than you expect?

If you can describe clearly and accurately what the problem you're seeing is, we will be more able to help you.

I'll note that at the start of your for loop, you ask the user to enter the hours and the pay rate. Then, you immediately have a loop which asks the user to do it again. Is that what you're talking about?
Basically you need only one for loop to get all the input.
Then another foor loop to do the calculations and output.
To get the hours and payrate it would be asier to write a function for them.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int getHours()
{
  // your code here
  return 0;
}

double getRate()
{
  // your code here
  return 0;
}

int main()
{

  const int empId = 7; // constant for the array size
  int workers[empId] = { 5658846, 4520125, 7895122, 8777541, 8451277, 1302850,7580489 };// Giving 7
  int hours[empId] = {0};
  double payRate[empId] = {0};


  cout << "Please enter the hours worked by the employees and their their hourly pay rates.\n";
  
  // get all the input
  
  for (int index = 0; index < empId; index++)
  {
    cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
    hours[index] = getHours();// user input
    cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
    payRate[index] = getRate();
  }
  
  cout << "This is the gross pay for each employee:\n";
  cout << fixed << showpoint << setprecision(2);

  // calc payment and output it

  for (int index = 0; index < empId; index++)
  {
    double grossPay = hours[index] * payRate[index];
    cout << "Employee #" << (index + 1);
    cout << ": earned $" << grossPay << endl << endl;
  }
  return 0;
}
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{

    const int empId = 7; // constant for the array size
    int workers[empId] = { 5658846, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 };// Giving 7
    int hours[empId];
    double payRate[empId];

    cout << "Please enter the hours worked by " << empId << " employees and their hourly pay rates.\n";

    for (int index = 0; index < empId; index++)
    {
        cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> hours[index];// user input
        cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> payRate[index];
        while( !(payRate[index] > 0.0 || payRate[index] <= 15.00) )
        {
             cout << "The pay rate must be at least $15.00. " << endl;
             cin >> payRate[index];
        }
    }

    cout << endl << "The gross pay for each employee:\n\n";

    for (int index = 0; index < empId; index++)
    {
        double grossPay = hours[index] * payRate[index];
        cout << "Employee #" << (index + 1) << endl;
        cout << "Gross: $" << showpoint << fixed << setprecision(2) << grossPay << endl << endl;
    }

    return 0;
}


I think this should do it
Topic archived. No new replies allowed.