hey, i am making a dungeoncrawler and was wondering what the best way to set up random movement for the monsters and moving the hero would be.

i am using a 9 by 9 array for the dungeon and moving them like this
hero
Code:
void heromove()
{
    int movehero2;

    char movehero;
    retry:;
    cout << "go" << endl;
    bool hmoved;
    hmoved = 1;
    cin >> movehero;
    if(movehero == 'w')
    {
        movehero2 = 1;
    }
    else if(movehero == 'a')
    {
        movehero2 = 2;
    }
    else if(movehero == 's')
    {
        movehero2 = 3;
    }
    else if(movehero == 'd')
    {
        movehero2 = 4;
    }

    switch(movehero2)
    {
        case 1:
        {
            dungeon[heroposition[0]][heroposition[1]] = 'O';
            heroposition[0]++;
            if(heroposition[0] < 0 || heroposition[0] >= 9)
            {
                cout << "you cant move through walls, try again.\n";
                heroposition[0]--;
                hmoved = 0;
            }
            dungeon[heroposition[0]][heroposition[1]] = 'H';
            break;
        }
        case 2:
        {
            dungeon[heroposition[0]][heroposition[1]] = 'O';
            heroposition[1]--;
            if(heroposition[1] < 0 || heroposition[1] >= 9)
            {
                cout << "you cant move through walls, try again.\n";
                heroposition[1]++;
                hmoved = 0;
            }
            dungeon[heroposition[0]][heroposition[1]] = 'H';
            break;
        }
        case 3:
        {
            dungeon[heroposition[0]][heroposition[1]] = 'O';
            heroposition[0]--;
            if(heroposition[0] < 0 || heroposition[0] >= 9)
            {
                cout << "you cant move through walls, try again.\n";
                heroposition[0]++;
                hmoved = 0;
            }
            dungeon[heroposition[0]][heroposition[1]] = 'H';
            break;
        }
        case 4:
        {
            dungeon[heroposition[0]][heroposition[1]] = 'O';
            heroposition[1]++;
            if(heroposition[1] < 0 || heroposition[1] >= 9)
            {
                cout << "you cant move through walls, try again.\n";
                heroposition[1]--;
                hmoved = 0;
            }
            dungeon[heroposition[0]][heroposition[1]] = 'H';
            break;
        }
        default:
        {
            cout << "that is not valid input, try again.\n";
            hmoved = 0;
            break;
        }
    }
    if(hmoved == 0)
    {
        goto retry;
    }

    return;
}
and doing something similar for the monsters, but using rand and time("NULL")

how can i improve these and what keeps it from working well?
i have some times where it will run fine and other times it will just return
go
go
go
go
go
thanks for any help