Not obtaining array values

I'm pretty sure I have the rest of my program down, but this one part I can't figure out.

I can compile and run my program, but it crashes when it gets to line 84, which references to the function on line 205, which is prototyped on line 18. The values for lowAverageID should be being obtained on line 78, but I am guessing I am not coding it correctly, which makes it not obtain the values properly, which is causing the program to crash.

My goal is to display employee IDs who receive less than the average amount of gross pay.

Too many characters so here is the pastebin link

http://pastebin.com/iqnYUiUc
double lowAverageID[4];
Array lowAverageID has four elements, the first is
lowAverageID[0] and the last is lowAverageID[3]

But the following function uses index values in the range 1 to 4. That is, the first element is ignored, and then an attempt is made to use a element outside the bounds of the array.
1
2
3
4
5
6
7
8
void displaylowGrossID(double lowAverageID[])
{
    for (int x = 1; x <= 4; x++)
    {
        std::cout << "\nEmployees With Below Average Gross Pay: "; 
        std::cout << lowAverageID[x] << endl;
    }
} 
Thanks, that makes sense!

Now with the revision, it will display one lowAverageID element, but it is junk. the second element it tries to display makes the program crash
I think my coding at line 76 may be the issue. That's the only thing that I can think of.

1
2
3
4
if( grossPay[x] < averageGrossPay )
               {
                   lowAverageID[x] = employeeID[x];
               } 
So everything seems to be working properly, except when it comes time to display the employee IDs with below average gross pay, it crashes when and only when there are 3 values to be displayed.

1, 2, and 4 separate values will display fine, but it crashes when there are three. The program is ran for 5 different employees, so 4 is the maximum who can be below average.

lowAverageIDs are determined on line 78, function is on line 207 and output is on line 87.

Why does it crash at 3???

http://pastebin.com/niJMYu0x
4 is the maximum who can be below average.
- but this can be any 4 of the 5. Even if there is only a single employee with below average gross pay, that could possibly be employee number 5.


This has four elements. Valid index values range from 0 to 3.
double lowAverageID[4];

This code is accessing elements with index 0 to 4. When element 4 is assigned, that is outside the bounds of the array, thus corruption of some other area of memory will occur with unpredictable consequences.
1
2
3
4
5
6
7
           //employees with lower than average gross pay
           for (int x = 0; x < 5; x++){    
           if( grossPay[x] < averageGrossPay )
               {
                   lowAverageID[x] = employeeID[x];
               }
               }


Solution: increase the size of lowAverageID[] to have 5 elements, not 4.
I don't understand this intention of this code:
206
207
208
209
210
211
212
213
214
// display employess with below average gross pay
void displaylowGrossID(double lowAverageID[], double grossPay[])
{
    std::cout << "\nEmployees With Below Average Gross Pay:\n";
    for (int x = 0; x < lowAverageID[x]; x++)
    {
        cout << lowAverageID[x] << "  --  $" << grossPay[x] << endl;
    }
}


At line 210, the integer x is compared with the floating-point value in array location lowAverageID[x]. This location may contain a valid employee id, or else it could contain uninitialised data (effectively a random value in the range -inf to +inf, or nan).

The loop may exit prematurely, before all the values were output, or else it may fail to exit before outputting spurious values.

The design of the mechanism here I think needs to be reconsidered. One possibility is to set the value to zero if it is not used, and then loop through all five values, but print the result only if it is non-zero.

There are other solutions, including the use of a vector which would start out empty, and contain only the required data.
Topic archived. No new replies allowed.