CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Array question

  1. #1
    Join Date
    Nov 2008
    Posts
    33

    Array question

    Why does this program only display the last value in the array and not all 10?

    PHP Code:
    #include <iostream>
    using namespace std;

    int main()
    {
        const 
    int NUM_MEN 10;
        
    int fish[NUM_MEN], cought 0;

        for (
    int count 0count NUM_MENcount++)
        {
            
    cout << "How many fish did fisherman " << count+<< " catch? ";
            
    cin >> fish[cought];
        }

        for (
    int count 0count NUM_MENcount++)
        {
            
    cout << "\nFisherman " << count+<< " cought " << fish[cought] << " fish" << endl << endl;
        }
        return 
    0;


  2. #2
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    Re: Array question

    Because:
    1) When you populated the array you forgot to increment "cought". The amounts for all fishermen are stored in fish[0];

    2) You should print fish[count] not fish[cought]. cought is always 0, and I don't think you should even use it, since you have the for loop counter.

  3. #3
    Join Date
    Nov 2008
    Posts
    33

    Re: Array question

    Quote Originally Posted by marceln View Post
    Because:
    1) When you populated the array you forgot to increment "cought". The amounts for all fishermen are stored in fish[0];

    2) You should print fish[count] not fish[cought]. cought is always 0, and I don't think you should even use it, since you have the for loop counter.
    ok so i change cought to count, delcare int count at the beginning of the program, but what i don't understand is, isn't count just a counter for the for loop?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Array question

    Sure count is a counter for the loop. I'm not sure what your point is though.....?

  5. #5
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Array question

    If you look at this loop
    Code:
        for (int count = 0; count < NUM_MEN; count++) 
        { 
            cout << "How many fish did fisherman " << count+1 << " catch? "; 
            cin >> fish[cought]; 
        }
    you are putting the answer into fish[cought], but cought is never being incremented, so in essence, the number of fish cought for each of the 10 fishermen is being stored in fish[0], with each one overwriting the previous one.

    The same is true for this loop
    Code:
        for (int count = 0; count < NUM_MEN; count++) 
        { 
            cout << "\nFisherman " << count+1 << " cought " << fish[cought] << " fish" << endl << endl; 
        }
    In both cases, you need to have
    Code:
    fish[count]
    instead of
    Code:
    fish[cought]
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  6. #6
    Join Date
    Nov 2008
    Posts
    33

    Re: Array question

    ok, I think I understand it a little better. Think I need to find a place on the web where I can do some more reading on how arrays and loops work together, separately I understand them, but putting them together is where I have trouble.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Array question

    Learning programming from web sites is likely to prove to be an exercise in futility. Get a good book that's been peer reviewed, is widely accepted and takes you through the steps in a logical order.

    There's been an unfortunate increase in people trying to learn for putting bits and pieces they find on various web site together. Most of them just end up confused and misguided.

    FWIW, caught is spelled with an "a", not an "o".

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured