views:

128

answers:

4

I've no idea if this is too newbie or generic for stackoverlflow. Apologies if that's the case, I don't intend to waste time.

I've just started working through C++ Primer Plus and I've hit a little stump. This is probably super laughable but:

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 3 years. The rest of the code works fine: input is fine, individual yearly output is fine but I just can't get 3 years added together for one final total.

Sample data would be entering 1 for every option, to give me 3 totals of 12:

year 1: 12
year 2: 12
year 3: 12

The 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. I've been messing with it for God knows how long.

Any idea guys? Sorry if this isn't the place!

+1  A: 

It looks like you're resetting absoluteTotal each iteration. Do you really want that?

Maybe this would be what you want?:

absoluteTotal += (yearlyTotal[year][year] += sales[year][month]);
Christopher Bertels
You're right, and I figured that as well.The output wasn't what I expected. Let me edit the post to include all the code, it'll probably help.
aLostMonkey
Amending the code so it uses the assignment by addition operator results in the following output: Yearly sales: Year 1: 12 Year 2: 12 Year 3: 12 The total number of books sold over a period of 3 years is: 234 Not sure where that 234 comes from. :
aLostMonkey
+1  A: 
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
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] = {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++)
    {
        yearlyTotal[year] += sales[year][month];
    }
    absoluteTotal += yearlyTotal[year];

    cout << yearlyTotal[year] << endl;

}

cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
return 0;
}
  1. You only need to increment the absoluteTotal outside of the per month count
  2. You only need a one dimensional array for the yearly count.

When it comes to things like this it helps to write them out on paper first.

Good Person
I will continue to watch answers and comments here and I'll improve my answer as needed.
Good Person
thanks. I'm using a two-dimensional array because that's what the question asked for. That said, probably not every aspect needed the two-dimensional array. I did draft things out on paper but I think I was getting too bogged down. I should have taken a break and come back with a clear mind. Thanks man.
aLostMonkey
@aLostMonkey: See the check mark next to the question? You can accept the answer that way.
Good Person
done. I've never took part here, just read a few posts. I tend to lurk over at the programming sub-reddits and find myself here often.
aLostMonkey
@aLostMonkey: welcome to StackOverflow and I hope you have a good flight ;)
Good Person
A: 

The problem is where you have your absoluteTotal increment you're counting the earlier months multiple times (since yearlyTotal is a counter, it increments each month, so adding it onto absoluteTotal every time counts month 1 12 times, month 2 11 times, etc).

Instead, you want that loop to look like this:

for (int year = 0; year < YEARS; year++)
{
    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        (yearlyTotal[year][year] += sales[year][month]);

    }

    absoluteTotal += yearlyTotal[year][year]; 
    cout << yearlyTotal[year][year] << endl;

}

So that you count each month only once.

EDIT: Good Person's comment about only needing a 1D array is also correct, of course. :)

Donnie
A: 

Check the code below. It will work.

int yearlyTotal[YEARS];
int absoluteTotal = 0;

cout << "Yearly sales:" << endl;

for (int year = 0; year < YEARS; year++)
{
    yearlyTotal[year] = 0;

    cout << "Year " << year + 1 << ":";

    for (int month = 0; month < MONTHS; month++)
    {
        yearlyTotal[year] += sales[year][month];
        absoluteTotal += sales[year][month];
    }

    cout << yearlyTotal[year] << endl;

}

cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
Jorg B Jorge