Stack around the variable ‘ ‘ was corrupted

location is an array of a single char.
There is no location[1].ShareImprove this answer Follow answered Feb 27 ’11 at 21:00 SLaks 825k171171 gold badges18511851 silver badges19261926 bronze badges Add a comment 7

You are prompting the memory address of location array to your user. You should ask location indices separately:

void GameBoard::enterShips()
{
    int location[2];
    int ships = 0;
    int count = 1;

    while(ships < NUM_SHIPS)
    {
        cout << "Enter a location for Ship " << count << ": ";
        cin >> location[0];
        cin >> location[1];
        cout << endl;

        Grid[location[0]][location[1]] = SHIP;
        ships++;
        count++;
    }
}

Notice int location[2]; since an array of size 1 can only hold one element. I also changed the element type to int. Reading char’s from the console will result in ASCII values, which are probably not what you want.

Leave a Comment