I'm trying to create the code for a paycheck program where I am paid double time for anything over 10 hours, time and a half for 8-10 hours and straight time for 0-8 hours. For some reason the pay isn't accumulating correctly. Any help would be appreciated.

``````#include <iostream>
using std::cout;
using std::cin;

#include <iomanip>
using std::endl;

int main()
{
double Array[13];
double pay = 0;
double payRate = 19.52;

for (int i = 0; i <= 12; i++)
Array[i] = 0;

for (int j = 1; j <= 12; j++)
{
cout << "Enter hours for day " << j << endl;
cin >> Array[j];
}

for (int k = 1; k <= 12; k++)
{
if (Array[ k ] > 10)
pay = ((payRate * 2 * (Array[ k ] - 10)) + (payRate * 1.5 * 2) + (payRate * 8));
else if (Array[ k ] > 8)
pay = ((payRate * 1.5 * (Array[ k ] - 8)) + ( payRate * 8));
else if (Array[ k ] >= 0)
pay = payRate * 8;
else cout << "Hours entered incorrectly, Please try again!\n" << endl;
pay += pay;
}
cout << pay;
return 0;
}``````

## All 4 Replies

Shouldn't your second for loop be like this?

``````for (int j = 0; j <= 12; j++)
{
cout << "Enter hours for day " << j+1 << endl;
cin >> Array[j];
}``````

It might be easier to accumulate the pay with

``````set totalpay to hours * straighttime       // this will include ALL hours worked
subtract 8 from hours to get all overtime  // time+half and double
add to totalpay hours * time-and-a-half    // IOW 1/2 of straighttime
subtract 2 from hours to get all doubletime
add to totalpay hours * time-and-a-half    //another 1/2 of straighttime
// 2 halves = one whole``````

Shouldn't your second for loop be like this?

I am a novice programmer and I didn't think of that. Is it better or more common to do it that way?

I just skipped the zeroeth subscript so the first workday would be 1

I am a novice programmer and I didn't think of that. Is it better or more common to do it that way?

I just skipped the zeroeth subscript so the first workday would be 1

Yes, it's more common to actually use the 0th element. If you need 12 elements, it makes less sense to define 13 of them. Especially in the case of a structure that might be -- say -- 1000 total bytes. A big waste of space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.