I’ve just started working through C++ Primer Plus and I have hit a little stump.
const int MONTHS = 12;
const int YEARS = 3;
int sales[YEARS][MONTHS] = {0};
const string months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
for (int year = 0; year < YEARS; year++)
{
for (int month = 0; month < MONTHS; month++)
{
cout << "Please enter year " << year + 1 << " book sales for the month of " << months[month] << ": \t";
cin >> sales[year][month];
}
}
int yearlyTotal[YEARS][3] = {0};
int absoluteTotal = 0;
cout << "Yearly sales:" << endl;
for (int year = 0; year < YEARS; year++)
{
cout << "Year " << year + 1 << ":";
for (int month = 0; month < MONTHS; month++)
{
absoluteTotal = (yearlyTotal[year][year] += sales[year][month]);
}
cout << yearlyTotal[year][year] << endl;
}
cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
I wish to display the total of all three years. The rest of the code works fine: input is fine, individual yearly output is fine but I just can’t get three years added together for one final total.
Sample data would be entering 1 for every option, to give me three totals of 12:
year 1: 12
year 2: 12
year 3: 12The total number of books sold over a period of 3 years is: 12
The final 12 should obviously be 36.
I did have the total working at one point but I didn’t have the individual totals working. I messed with it and reversed the situation.
When it comes to things like this it helps to write them out on paper first.