Simple array program not working!!! i dont know why?!

I am trying to store values from the user into an array and then display them it seems really simple and I do not know why it is not working. I can store the values fine but when I try to display them the first two values show garbage numbers.When looking at the code the first loop iterates fine but the second loop doesnt. Please help. Here is my code.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
const int NUM_EMPLOYEES=7;
const long empId[NUM_EMPLOYEES]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
double hours[6],
payRate[6],
wages[6];

cout<<fixed<<showpoint<<setprecision(2);
for(int count=0;count<NUM_EMPLOYEES;count++)
{
cout<<"Employee ID: "<<empId[count]<<endl;
cout<<"Enter the amount of hours worked and the pay rate"<<endl;
cout<<"hours: ";
cin>>hours[count];
cout<<"Pay Rate: $";
cin>>payRate[count];
wages[count]=hours[count]*payRate[count];
cout<<wages[count]<<endl;
}



for(int count=0;count<NUM_EMPLOYEES;count++)
{
cout<<"Employee ID: "<<empId[count]<<endl;
cout<<"Wages: $"<<wages[count]<<endl;
}

return 0;
}
http://www.cplusplus.com/articles/z13hAqkS/


Please always use code tags.

count exists in the std namespace as std::count, try removing using namespace std; and change all the instances of cout, cin, fixed, showpoint, setprecision, endl to have std:: before each one.

Does that make any difference?
my teacher always wants us to use using namespace std;
for whatever reason the values stored in wage[count] and the value stored in empId[count] both get changed after the first loop and I dont know why. And not all the values get changed only the first 2 get changed
Well that is sad, your code demonstrates a very good reason not to do that.

You could change the name of the variable to something else, but that is a cop out IMO.

Does your teacher say why to use it? I would be interested to know what s/he says - it is almost certainly wrong.

Bringing in an entire namespace into the global namespace defeats the purpose of namespaces. That is, is causes naming conflicts, as you have just discovered.

All the experienced and expert coders, all put std:: before each std thing. Are you explicitly going to loose marks for doing it? If so, that is even more sad.
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
#include <iostream>
#include <iomanip>

//using namespace std;

int main ()
{
const int NUM_EMPLOYEES=7;
const long empId[NUM_EMPLOYEES]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
double hours[6],
payRate[6],
wages[6];

std::cout<<std::fixed<<std::showpoint<<std::setprecision(2);
for(int Item=0;Item<NUM_EMPLOYEES;Item++)
{
std::cout<<"Employee ID: "<<empId[count]<< "\n";
std::cout<<"Enter the amount of hours worked and the pay rate\n";
std::cout<<"hours: ";
std::cin>>hours[count];
std::cout<<"Pay Rate: $";
std::cin>>payRate[count];
wages[count]=hours[count]*payRate[count];
std::cout<<wages[count]<< "\n";
}



for(int Item=0;Item<NUM_EMPLOYEES;Item++)
{
std::cout<<"Employee ID: "<<empId[count] << "\n";
std::cout<<"Wages: $"<<wages[count] << "\n";
}

return 0;
}


Arrays go from 0 to size -1, you are over running the array, make use of the const variable.

So you need to change something to fix that code.
Last edited on
Hi zacbenzerara,

You taken data for 7 employees, but your hours, payRate and wages arrays is only for 6! WHY?

Since you have
 
const int NUM_EMPLOYEES = 7;


Why not use the variable for other arrays, like so:
 
double hours[NUM_EMPLOYEES], payRate[NUM_EMPLOYEES], wages[NUM_EMPLOYEES];


@TheIdeasMan: Sorry, I have to disagree with you on the point of using namespace std;, as the reason for incorrect values. Even without "Bringing in an entire namespace into the global namespace", you still have the same issues!

More so, I have read experience coder use using namespace std;
teez wrote:
@TheIdeasMan: Sorry, I have to disagree with you on the point of using namespace std;, as the reason for incorrect values. Even without "Bringing in an entire namespace into the global namespace", you still have the same issues!

More so, I have read experience coder use using namespace std;


It turned out not to be the reason, but it is still bad.

In terms of experienced users doing it, well nearly everyone seems to do it: Lecturers, authors, even Stroustrup himself, but it is still bad. Look at the experienced and experts users on this forum, the all do std::

The thing is, I and many others have valid reasons for holding this view, one can Google to see plenty of them.
Topic archived. No new replies allowed.