Debug

can someone help me debug this please.



// accumulates total sales from each of 10 salespeople
#include<iostream.h>
#include<conio.h>
void main()
{
double earnings[10] = {0.0};
double amt;
int person;
cout<<"Enter salesperson number 1 - 10. Enter 99 to quit. ";
cin>>person;
while(person != 99)
{
cout<<"Enter the sale amount ";
cin>>amt;
earnings[person] = amt;
cout<<"Enter another salesperson numbr 1 -10 or 99 to quit ";
cin>>person;
}
cout<<"Salesperson totals are:"<<endl;
for(person = 0; person < 10; ++person)
cout<<"Salesperson "<<(person + 1)<<" $"<<earnings<<endl;
getch();
}

The problem is that the index 10 is out of bounds. Hence (when you enter 1 - 10):

earnings[person - 1] = amt; // Note: - 1

You need to subtract 1 because the highest valid index is 9.
Topic archived. No new replies allowed.