C++ Code HELP!

Questions about programming languages and debugging
Post Reply
User avatar
truly9ja
Newbie
Newbie
Posts: 9
Joined: 20 Aug 2009, 16:00
14

C++ Code HELP!

Post by truly9ja »

Below is a program to input and print the total number of books sold every month for three years. Now, i can't display the sum of the total number of books sold in a particular year, but can only print the total sum of all the books sold for 3 years. I know I'm not doing the proper thing. I'm a beginner, I really need help to continue my reading. Thanks

Code: Select all

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    int counter1 = 2000;
    int SumSold[3][12];
    int NumSold[3];
    string months[3][12] =
    {

        {
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
        },
        {
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
        },
        {
            "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"
        }
    };

    cout << "NO. OF BOOKS SOLD FROM 2001 - 2003 " << endl << endl;
    for(int i = 0; i < 3; ++i)
    {
        ++counter1;
        cout << "YEAR " << counter1 << endl;
        for(int ix = 0; ix < 12; ++ix)
        {
            cout << "Books sold in " << months[i][ix] << ": ";
            cin >> NumSold[ix];
            SumSold[i][ix] = SumSold[i][ix] + NumSold[ix];
        }
    }

   for(int iv = 0; iv < 3; ++iv)
   {
       for(int inval = 0; inval = 12; ++inval)
       {
           cout << SumSold[iv][inval] << endl;
       }
   }
   return 0;
}

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

To count a specific year you could do something like this

Code: Select all

int year = 1; //the year to sum up
int sum  = 0;

cout << "YEAR: " << year << endl;

for(int i = 0; i < 12; ++i)
        sum += SumSold[year][i];
As in simply specify a constant number for the "year" index of the two dimensional array, instead of looping through all of it.

Didn't read all of the code very closely so the example above most likely isn't compatible with your code, but I hope you at least get the picture ;)
"The best place to hide a tree, is in a forest"

User avatar
truly9ja
Newbie
Newbie
Posts: 9
Joined: 20 Aug 2009, 16:00
14

Post by truly9ja »

Thanks for your input. But where should that code go in my code. I'm a bit confuse.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

truly9ja wrote:Thanks for your input. But where should that code go in my code. I'm a bit confuse.
That was just a small example, where you put the code is entirely up to you, and how you want the program to function ^^

One of the largest parts of programming is solving problems. I gave you the hammer, now figure it out! =)
"The best place to hide a tree, is in a forest"

Post Reply