Question

Everything seems to be okay but when I enter I it says

Unhandled exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

First-chance exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005:     Access violation writing location 0xCCCCCCCC.
Unhandled exception at 0x64A1EB90 (msvcr110d.dll) in ConsoleGame1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.
The program '[5088] ConsoleGame1.exe' has exited with code 0 (0x0).

The code:

void Inventory();

struct Item
{
    string itemName;
    string itemDescription;
    int itemNumber;
    bool haveItem;

    void DisplayItem();
};

int main()
{
    char inv;
hint:
    cout << "HINT: To open your inventory  press 'I'.\n";
    cin >> inv;
    if (inv=='I') Inventory();
    else goto hint;
    system("pause");
    return 0;
}

void Inventory()
{
    Item Letter =
    {
        Letter.itemName = "Letter",
        Letter.itemDescription = "...",
        Letter.itemNumber = 1,
        Letter.haveItem = true
    };
    Item Wood =
    {
        Wood.itemName = "Wood",
        Wood.itemDescription = "Birch wood.",
        Wood.itemNumber = 2,
        Wood.haveItem = false
    };
    Letter.DisplayItem();
    Wood.DisplayItem();
}
Was it helpful?

Solution

To address the problem at hand, you're assigning into objects which aren't constructed yet:

Item Letter =
{
    Letter.itemName = "Letter",
    Letter.itemDescription = "...",
    Letter.itemNumber = 1,
    Letter.haveItem = true
};

You're assigning into members of Letter when specifying the arguments for initialisng Letter. That won't do. What you were after is:

Item Letter =
{
    "Letter",
    "...",
    1,
    true
};

However, the code in general shows you'd better start from the basics with a good book to guide you, as I've said in the comments. For example, you definitely do not want to use goto instead of a loop. And the Item class could use a constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top