I would like the user to be able to see the sales total for any given month when they input a valid month. Everything works other than what I stated above. I am completely stumped on how to store the salesTotal values inputted by the user into an array (monthSalesTotal). Here is what i have so far, any help will be greatly appreciated.

``````#include <iostream>
using namespace std;

int main()
{
int i;
int salesTotal[12];
double average = 0.0;
double sum = 0.0;
int choice;
int monthSalesTotal[12];

for (i = 1; i < 13; i++)
{
cout << "Please enter the monthly sales total for month " << i << ":> ";
cin >> salesTotal[i];
sum += salesTotal[i];
average = sum / i;
}
cout << "The average monthly sales for the year is:> " << average << endl;

cout << "For what month would you like to see a sales value? ";
cin >> choice;

switch(choice)
{
case 1:
choice = 1;
break;
case 2:
choice = 2;
break;
case 3:
choice = 3;
break;
case 4:
choice = 4;
break;
case 5:
choice = 5;
break;
case 6:
choice = 6;
break;
case 7:
choice = 7;
break;
case 8:
choice = 8;
break;
case 9:
choice = 9;
break;
case 10:
choice = 10;
break;
case 11:
choice = 11;
break;
case 12:
choice = 12;
break;
default:
cout << "This is an invaid choice, please re-enter:> ";
cin >> choice;
}
cout << "The sales total for the month " << choice << " is:> " << salesTotal[i] << endl;

system("pause");
return 0;
}``````

## All 4 Replies

The first thing I see is that valid indices for salesTotal range from 0-11 but you are trying to access elements with indexes of 1-12. Adjust the indicies so they coincide and don't access memory not allocated to salesTotal and it has a good chance of working.

as lerner said....
for(int i = 0; i < 12; i ++)

arrays start with 0, the [12] is a null which is used to indicate the end of the array.

please mark thread as solved on bottom

ok cheers guys, much appreciated!

arrays start with 0, the [12] is a null which is used to indicate the end of the array.

No, the [12] is not a null, it's the next variable in the data area. [11] is the end of any array with 12 elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.