Stack around the variable ' ' was corrupted

前端 未结 4 1195
孤城傲影
孤城傲影 2021-01-12 10:09
void GameBoard::enterShips()
{
    char location[1];
    int ships = 0;
    int count = 1;

    while(ships < NUM_SHIPS)
    {
        cout << \"Enter a loc         


        
4条回答
  •  执笔经年
    2021-01-12 10:57

    You made the location variable only able to hold a single character. You access it expecting it to hold at least 2 characters. If you're using cin and expecting to read exactly two characters, a better method would be:

    char locationX, locationY;
    // ...
    std::cin >> locationX >> locationY;
    // ...
    Grid[locationX][locationY] = SHIP;
    

提交回复
热议问题