|
-
January 7th, 2009, 11:17 PM
#1
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 = 0; count < NUM_MEN; count++)
{
cout << "How many fish did fisherman " << count+1 << " catch? ";
cin >> fish[cought];
}
for (int count = 0; count < NUM_MEN; count++)
{
cout << "\nFisherman " << count+1 << " cought " << fish[cought] << " fish" << endl << endl;
}
return 0;
}
-
January 7th, 2009, 11:24 PM
#2
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.
-
January 7th, 2009, 11:31 PM
#3
Re: Array question
 Originally Posted by marceln
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?
-
January 8th, 2009, 12:03 AM
#4
Re: Array question
Sure count is a counter for the loop. I'm not sure what your point is though.....?
-
January 8th, 2009, 06:57 AM
#5
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 haveinstead ofHope 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
-
January 8th, 2009, 07:28 AM
#6
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.
-
January 8th, 2009, 08:12 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|