Array of char not changing values

closed account (EwCjE3v7)
If you look at the Winner function, and than go to the for loop at the bottom, it changed the value of the array that is defined in the main function and has a reference to it in the winner, so that dosnt changed the value. Do the following when running the program.

1. put any names for the players.
2 Make sure the first user(X) wins by and it needs to win by place its values in the following places 1,5 and 9
3. Once the winner function is called type in yes that you want to play again
Pictures: http://share.pho.to/5LYj1
As you can see now the board isnt printed

http://codeviewer.org/view/code:3f01
Last edited on
closed account (EwCjE3v7)
Anyone?
The array has 9 elements? In your for loop you increment the index up to 10 before stopping. The largest element index is 8, so it seems like you go out of bounds here on the array.

char places[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

1
2
3
for (unsigned index = 0; index != 10; ++index, ++plcval) {
rplaces[index] = plcval;
}
closed account (EwCjE3v7)
Yes thats an error to but is not the problem, as a tried a for earlier
for (...:....)
But i think the error here is that when i pass the areay, im actually passing a pointer to the first element
Last edited on
If you use the arrayName in the function argument, that's essentially a pointer to the first element. Did you just want to send one element, not the whole array?

Usually when sending arrays to functions, I put the arrayName and a separate variable for the size.
myfunction(arrayName, size)

Then prototype/definition of the called function, I have empty brackets (not brackets with the size of the array like you have here bool winner(char (&rplaces)[9], string &rp1, string &rp2)) besides the array name to indicate the function is receiving an array, and the separate size variable.

function(arrayName[], size).
closed account (EwCjE3v7)
No i would like to pass the whole array as a pointer or reference so I can modify it. If u look, it primts out the board in the winner funtion correctly but not after u enter yes.
And when i put the for loop in the main funtion same thing happens

EDIT:

I believe I have found the error, a really stupid one, if you look I`m assing the value of plcval which is not a char to a char, without the '' . so its changing it to a char and that is my error, I need to for ex say rpalce[index] = '1';
Last edited on
Yep - there's the char/int discrepancy there.

Line 95 - do you mean to have an assignment operator there instead of ==?

Also - you call the main function a couple of times. Main should not be called as a function.
Topic archived. No new replies allowed.