I am attempting to write a few basic card-game programs, and to do so, I thought I'd generate a deck of cards. I've provided the code which I've written to do so below:
This code returns an error stating that 'the stack around variable "deck" was corrupted."
Being somewhat new to C++ I'm not overly confident with what this means or how to overcome it. Please excuse me if it is a very simple error as, like I have stated, I am a newbie. Also I'm sure there are many better ways to generate a deck of cards. I think the recurring if statements are most likely causing me an issue yet I'm not sure how to write the program without them.
Any help is greatly appreciated. Thanks in advance, James.
On lines 14 to 16. You declared an array of size 53 (which is the wrong number of cards in a deck) which means the valid indices for the array are from 0 to 52 (deck[0] to deck[52]). However, your for loop continues an iteration when counter = 53, so you attempt to access deck[53], which is one step out of the array boundaries. You grabbed something you weren't supposed to and modified it, corrupting the stack.
You should reconsider your design, so you don't have to use so many else if statements.
Being somewhat new to C++ I'm not overly confident with what this means or how to overcome it.
Usually it means you are stepping outside the bounds of an array. In this case, the error suggests that the array is "deck".
If you note, you are stepping out of bounds here:
1 2 3
int deck[53]; // <- deck is size [53], which means [52] is last legal element to access
for(int counter = 0; counter <= 53; ++counter)
deck[counter] = counter; // <- accessing element [53].. OUT OF BOUNDS