Stack around the variable ' ' was corrupted

前端 未结 4 1225
孤城傲影
孤城傲影 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 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.

提交回复
热议问题