loop and array problem for beginner. Please HELP!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: loop and array problem for beginner. Please HELP!

  1. #1
    Join Date
    Sep 2009
    Posts
    5

    loop and array problem for beginner. Please HELP!

    The code below is a program to input and print a number of books sold for three years. I'm not able to print the sum of books sold for each year, but I can print the total number of books sold for the 3years. I'm a beginner. I need help to enable continue my reading.
    I will appreciate if you can throw more lights on array that could be or help.
    Thanks

    #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;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: loop and array problem for beginner. Please HELP!

    It doesn't seem terribly useful to duplicate the months 3 times like that. It's not like they're about to change or anything.

  3. #3
    Join Date
    Sep 2009
    Posts
    5

    Re: loop and array problem for beginner. Please HELP!

    Good point sir. Thanks alot

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: loop and array problem for beginner. Please HELP!

    You need to initialize your numeric arrays to contain 0s. You seem to be assuming they will for now, but that's not guaranteed. Just adding "= {0};" after the declaration should do it.

  5. #5
    Join Date
    Sep 2009
    Posts
    5

    Re: loop and array problem for beginner. Please HELP!

    Another important point.
    Thanks.
    Is it possible to add an array(1 or 2D) to an integer and assign the result to an integer?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: loop and array problem for beginner. Please HELP!

    You mean sum up the values in the array? You could use std::accumulate, but for a beginner it's probably best to just use a loop. That's what accumulate() is doing internally anyway.

  7. #7
    Join Date
    Sep 2009
    Posts
    5

    Re: loop and array problem for beginner. Please HELP!

    Thank you sir!

  8. #8
    Join Date
    Sep 2009
    Location
    Kent, UK
    Posts
    8

    Question Re: loop and array problem for beginner. Please HELP!

    Im just interested to know why you used "endl" twice in the 'number of books sold from 2001-2003 bit, or is that just a mistake. Sorry Im a total noob at C++.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center