can someone help me with an array

i'm getting some odd out put from this one. I can't really explain it

// problem using an array

#include<iostream>
using namespace std;

int main()
{
const int SIZE = 10;
double earnings[SIZE];
const int STOP = 99;
double amt;
int person;
cout << "Enter sales made by any of our ten salespeople" << endl <<
"Their sales numbers are digits 1 through 10" << endl <<
"You can enter as many money amounts as you want " << endl <<
"for each person in any order" << endl;
cout << "Enter salesperson number 1 - " << SIZE << ". Enter " << STOP << " to quit. ";
cin >> person;
while(person != STOP)
{
if(person >= 1 && person <= 10)
{
cout << "Enter the sale amount ";
cin >> amt;
earnings[person] += amt;
}
else
cout << "Number out of range. Please enter another" << endl;
cout << "Enter another salesperson number 1 - 10 or 99 to quit ";
cin >> person;
}
cout << endl << "Salesperson totals are:" << endl;
for(person = 0; person < SIZE; person++)
cout << "Salesperson " << (person + 1) << " $" << earnings[person] << endl;

system("pause");
return 0;
}
> I can't really explain it
*sigh*
The code should look like this:

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>
using namespace std;

int main()
{
const int SIZE = 10;
double earnings[SIZE] = {0};
const int STOP = 99;
double amt;
int person;
cout << "Enter sales made by any of our ten salespeople" << endl << 
"Their sales numbers are digits 1 through 10" << endl << 
"You can enter as many money amounts as you want " << endl << 
"for each person in any order" << endl;
cout << "Enter salesperson number 1 - " << SIZE << ". Enter " << STOP << " to quit. ";
cin >> person;
while(person != STOP)
{
if(person >= 1 && person <= 10)
{
cout << "Enter the sale amount ";
cin >> amt;
earnings[person - 1] += amt;
}
else
cout << "Number out of range. Please enter another" << endl;
cout << "Enter another salesperson number 1 - 10 or 99 to quit ";
cin >> person;
}
cout << endl << "Salesperson totals are:" << endl;
for(int person = 0; person < SIZE; person++)
cout << "Salesperson " << (person + 1) << " $" << earnings[person] << endl;


return 0;
}




The problem was, you needed to initialize all values of the array to zero with the line
double earnings[SIZE] = {0};

second, in the loop:
1
2
3
4
5
6
if(person >= 1 && person <= 10)
{
cout << "Enter the sale amount ";
cin >> amt;
earnings[person] += amt;
}
The array is not being set from 0-9 as it should be. You are starting the input of values at 1-10. Arrays always start at 0. so if the size is 10, it's actually 0-9. Therefore the code should look like this:

1
2
3
4
5
6
if(person >= 1 && person <= 10)
{
cout << "Enter the sale amount ";
cin >> amt;
earnings[person - 1] += amt;
}

There the first value for person, if the person is 1, is then put into the array at 0, instead of at 1 where it was in your code.



Does this make sense?
not initializing the values of the array to 0 will leave garbage values in them, if no new value is put in.
Topic archived. No new replies allowed.