❔ Populating an Array with a Loop and then being able to call indexes
I created a 52 card playing deck, and I know the loop works because the output is correct. The problem is all it does is print out all the cards in the deck. It's not populating the deck array with the results outside the loop.
The reason why that's a problem is because if I wanted to print the 5th card of the deck (deck[4]) I can't, because the array is still empty outside of the loop.
It's possible that you don't use loops and or arrays and or my main method is wrong. I'm still learning. But none of the resources I can find talk about something like this. It either talks about prints the loop (which is what I can do already) or it manually populates an array 1 at a time (which would take up memory, exactly what I am trying to avoid by doing a loop).
Any help would be greatly appreciated. Thank you
3 Replies
Where do you check the items in the deck?
When you leave the deckOfCards method, the arrays you created are out of scope and get deleted.
^ in the Main() program.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.