Runtime error when trying to display array of objects

0

So, I'm trying to create a card game in C++ and while trying to make sure that my arrays are working I'm getting a runtime error which says:

Exception thrown at 0x7AD940D9 (vcruntime140d.dll) in Sabacc.exe: 0xC0000005: Access violation writing location 0x0BFE1052.

I have no idea what this error actually means, but here is my code just so you can see what is going on:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class cards {
public:
    string suit;
    int number = 0;
};

int main(int argc, char** argv) {
    /*Initialise Arrays*/
    cards Card[60];

    //initialise Vars
    string suits[4] = { "Coins", "Flasks", "Sabers", "Staves" };
    int values[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    //Define Card Values
    for (int c = 1; c < 61; c++) {

        if (c >= 1 && c <= 15) {
            Card[c].suit = "Coins";
            Card[c].number = c;
        }
        else if (c >= 16 && c <= 30) {
            Card[c].suit = "Flasks";
            Card[c].number = c - 15;
        }
        else if (c >= 31 && c <= 45) {
            Card[c].suit = "Sabers";
            Card[c].number = c - 30;
        }
        else if (c >= 46 && c <= 61) {
            Card[c].suit = "Staves";
            Card[c].number = c - 45;
        }

    }

    for (int i = 1; i < 60; i++) {
        cout << Card[i].number << " " << Card[i].suit << endl;
    }

    return 0;
}

If you can help, that would be really great. I'm a complete beginner, so there is probably a really obvious answer that I'm missing.

c++
visual-studio
runtime-error
runtimeexception
asked on Stack Overflow Feb 25, 2021 by Ticbow

2 Answers

0
    for (int c = 1; c < 61; c++) {

off-by-one error here. The array cards Card[60]; has only 60 elements (Card[0] to Card[59]), so you must not access Card[60].

Since you are using only Card[1] to Card[59] in the loop for printing, so you should adjust your first loop according to this.

In other words, the condition c < 61 should be changed to c < 60.

Another option is changing cards Card[60]; to cards Card[61]; (add one more element) and (optionally) change the condition of the second loop i < 60 to i < 61.

Card[0] is not used, but I think it is OK to waste one element of a few bytes on modern PC that have enough memory to increase readability and/or simplicity by starting index from one.

answered on Stack Overflow Feb 25, 2021 by MikeCAT • edited Feb 25, 2021 by MikeCAT
0

the cards structure allocates indexes 0-59. In the first iteration the loop is okay but i modified a little for understanding better. You can understand which indexes are being allocated. In the print statement if you do not want to show the '0' you can start the loop from 1. Mainly the error was in line number 25 for (int c = 1; c < 61; c++) {

Take a look at the full code below:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or 
input loop */

class cards {
public:
  string suit;
  int number = 0;
 };

int main(int argc, char** argv) {
 /*Initialise Arrays*/
 cards Card[60]; //indexes 0-59 gets allocated

 //initialise Vars
string suits[4] = { "Coins", "Flasks", "Sabers", "Staves" };
int values[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

//Define Card Values
for (int c = 0; c < 60; c++) { //iterate 0-59 i.e 60 elements

    if (c >= 1 && c <= 15) {
        Card[c].suit = "Coins";
        Card[c].number = c;
    }
    else if (c >= 16 && c <= 30) {
        Card[c].suit = "Flasks";
        Card[c].number = c - 15;
    }
    else if (c >= 31 && c <= 45) {
        Card[c].suit = "Sabers";
        Card[c].number = c - 30;
    }
    else if (c >= 46 && c <= 61) {
        Card[c].suit = "Staves";
        Card[c].number = c - 45;
    }

}

for (int i = 0; i < 60; i++) { //prints 0-59 i.e 60 elements
    cout << Card[i].number << " " << Card[i].suit << endl;
}
return 0;

}

answered on Stack Overflow Feb 25, 2021 by Yeasir Hossain

User contributions licensed under CC BY-SA 3.0