#include<iostream>
usingnamespace std;
int main()
{
constint SIZE = 10;
double earnings[SIZE] = {0};
constint 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.