将记录传递给子程序参数C++

Passing Records to subprogram parameter C++

本文关键字:子程序参数 C++ 记录      更新时间:2023-10-16

如何将结构传递给子程序参数?当我调用checkBelow函数时,它会给我一个错误-"CheckBellow":无法将参数2从"playerInfo"转换为"char"我以前传递过记录数组,但这是我第一次尝试传递单个记录。

  struct playerInfo
{
    char playerName[81];
    char playerID;
};
void CheckBellow ( char board[][10], char playerID, int dropChoice );
int PlayerDrop();
void DisplayBoard ( char board[][10] );
int main()
{
    playerInfo playerOne, playerTwo;
    char board[9][10];
    int trueWidth = 7; 
    int trueLength = 6; 
    int dropChoice;
    cout << "Let's Play Connect 4" << endl << endl;
    cout << "Player One please enter your name: ";
    cin  >> playerOne.playerName;
    playerOne.playerID = 'X';
    cout << "Player Two please enter your name: ";
    cin  >> playerTwo.playerName;
    playerTwo.playerID = 'O';
    DisplayBoard( board );
    dropChoice = PlayerDrop();
    CheckBellow( board, playerTwo, dropChoice );
    DisplayBoard( board );

return 0;
}
void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice )
{
    int width = 7;
    int length = 6;
    do
    {
        if ( length == 0 )
        {
            cout << "That row is full, please enter a new row";
            dropChoice = PlayerDrop();
            length = 6;
        }
        if ( board[length][dropChoice] != 'X' && board[length][dropChoice] != 'O' )
            board[length][dropChoice] = activePlayer.playerID;
        else
            --length;
    }while ( board[length][dropChoice] != 'X' && board[length][dropChoice] != 'O' );
}

void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice )

与声明不匹配

void CheckBellow ( char board[][10], char playerID, int dropChoice );

修复声明,即第二个参数char->playerInfo