Passing the value of an int into an array function argument

Hey all. I’m working on a small C++ console based game in which I’m a little stuck on.

I am trying to pass the value of a variable into an array which is an argument of a function.

Let me explain. The player is able to make choices by either inputting 1, 2, or 3 which is stored in int playerChoice;. Once the player makes their choice, I am wanting the current value of playerChoice to go into an array which is defined as follows int StorePlayerChoices(int *playerchoices, int size). This function will then iterate over the array, print out each value as they are added to each element (up to 10), then perform some maths.

Currently, when I attempt to print out the array, it’s filled with random numbers (probably because it’s not initialised correctly), or it only states there is only one element when there should be 10.

Does anyone have any potential solutions for this? Also, sorry if I have explained this poorly.

I imagine you’re declaring the array inside of the function you’re using, which will erase the array and setup that variable as if it were new (and likely uninitialized) each time you call the function. Since that array would only be present in the scope of the function and gets erased once you leave that scope.

But to be sure, can you share your code?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms