Attempt to create a structure object creating an Access Violation Writing Error In Location 0x3FE00000

-2

I'm learning to use structures and making a text-based RPG, and I'm aware that items should be a class in my code, however, since I am using structures and I'm new to c++ and programming I do not understand what exactly has gone wrong with my code nor how to fix it, the error occurs where I try to create a structure object called player. (I would also like to apologize for my messy code, i'm messing around and will be cleaning it once I get what I have working.)

This is my first attempt, I haven't tried much because I'm unsure what exactly to try.

#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct item {
    double resistance, attack;
    string name;
};
item none, WoodenBoots, LeatherChestplate, WoodenShield, WoodenClub, WoodenSword;


struct mobs {
    int health, attack;
    double droprate, resistance;
    item drops[3];
};

item GetData(string object);


int main()
{
    none.attack = 0;
    none.resistance = 0;
    none.name = "none";
    WoodenBoots.resistance = .05;
    WoodenBoots.attack = 0;
    WoodenBoots.name = "Wooden Boots";
    LeatherChestplate.resistance = .2;
    LeatherChestplate.attack = 0;
    LeatherChestplate.name = "Leather Chestplate";
    WoodenShield.resistance = .1;
    WoodenShield.attack = 0;
    WoodenShield.name = "Wooden shield";
    WoodenClub.resistance = 0;
    WoodenClub.attack = 1.2;
    WoodenClub.name = "Wooden Club";
    WoodenSword.resistance = .05;
    WoodenSword.attack = 1.5;
    WoodenSword.name = "Wooden Sword";

    mobs goblin;
    goblin.attack = 1;
    goblin.health = 10;
    goblin.droprate = .5;
    goblin.resistance = .1;
    goblin.drops[1] = WoodenClub;
    goblin.drops[2] = WoodenShield;
    goblin.drops[3] = WoodenBoots;

    mobs Alpha_Goblin;
    Alpha_Goblin.attack = 2;
    Alpha_Goblin.health = 15;
    Alpha_Goblin.droprate = .5;
    Alpha_Goblin.resistance = .1;
    Alpha_Goblin.drops[1] = WoodenSword;
    Alpha_Goblin.drops[2] = WoodenShield;
    Alpha_Goblin.drops[3] = LeatherChestplate;

    struct pdata {
        item Playeritem[6];
        item PlayerWeapon;
    }player;

    player.PlayerWeapon = WoodenSword;
    cout << player.PlayerWeapon.name << endl;

    player.PlayerWeapon = GetData("weapon");
    player.Playeritem[0] = GetData("sheild");
    player.Playeritem[1] = GetData("head");
    player.Playeritem[2] = GetData("torso");
    player.Playeritem[3] = GetData("legs");
    player.Playeritem[4] = GetData("feet");
    player.Playeritem[5] = GetData("hands");
    string weapon = player.PlayerWeapon.name;
    cout << weapon << endl;
    system("pause");
}

I expect it to create the structure and to move on to defining the item values inside the struct. i get the following error on the last line of the struct pdata: Exception thrown at 0x57115139 (vcruntime140d.dll) in Structure.exe: 0xC0000005: Access violation writing location 0x3FE00000.

c++
structure

1 Answer

-1

In C++ arrays are indexed from zero to num_elements - 1

So if you declare

int myArray[3];

and then try to access:

myArray[3] = 5;

You are going to get undefined behavior that may manifest itself as an access violation depending on your compiler and debug settings.

In Visual Studio you can use the Exception Settings and check off access violation exceptions while running in debug, and have the execution halt before it is thrown.

answered on Stack Overflow Jan 4, 2019 by Christopher Pisz • edited Jan 4, 2019 by Christopher Pisz

User contributions licensed under CC BY-SA 3.0