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;
}
count exists in the std namespace as std::count, try removing usingnamespace std; and change all the instances of cout, cin, fixed, showpoint, setprecision, endl to have std:: before each one.
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.
@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;
@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.