为什么我的printf语句会同时弹出?并得到一个无限循环?

时间:2013-04-20 05:21:31

标签: c syntax while-loop infinite-loop

为什么当我启动程序时,我将首先执行第一个printf语句,然后输入,然后它同时执行两个printf语句。 这是导致无限循环的原因吗?

启动程序

Player 1: Choose your symbol: 
    a

这一部分,它们都同时输出

Player 2: Choose your symbol: 
player1, enter placement: 

然后我得到一个无限循环。是否由于同时输出?

include <stdio.h>
int check(char player);
void move(char player);
char board[3][3] ;



int main(void)
{
int first;
char player1, player2;

        printf("Player 1: Choose your symbol: \n");
        player1 = getchar();

        printf("Player 2: Choose your symbol: \n");
        player2 = getchar();




int i=0;
int win;char turn;
while(win == 0)
{
        if((i%2) == 0){
                turn = player1;
                move(player1);
                win = check(player1);
                print();}
        else {
                turn = player2;
                move(player2);
                win = check(player2);
                print();}
        i++;
}

        if (i == 8)
                printf("its a tie");
        else
                printf("the winner is %c", turn);

return 0;
}
/*printing the board that takes in a placement int*/
void print(void)
{
        int r;
        printf("\n");
        for (r = 0; r < 3; r++){
                printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]);
        if (r != 2)
                printf("___________\n");
        }
        printf("\n");
return;
}
/*check to see if someone won*/
int check(char player)
{
    int r, c;

    for ( r = 0 ; r <3 ; r++)
    {
        if ((board[r][0] == player) && (board[r][1] == player) && (board[r][2] == player))
            return 1;
    }

    for ( c = 0 ; c <3 ; c++)
    {
        if ((board[0][c] == player) && (board[1][c] == player) && (board[2][c] == player))
            return 1;
    }

    if((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player))
        return 1;

    if((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player))
        return 1;

    return 0;
}

void move(char player)
{
    int place;
    printf("player1, enter placement: \n");
    scanf("%d", &place);

    if (place == 1)
        board[0][0] = player;
    else if (place == 2)
        board[0][1] = player;
    else if (place == 3)
        board[0][2] = player;

    else if (place == 4)
        board[1][0] = player;
    else if (place == 5)
        board[1][1] = player;
    else if (place == 6)
        board[1][2] = player;

    else if (place == 7)
        board[2][0] = player;
    else if (place == 8)
        board[2][1] = player;
    else if (place == 9)
        board[2][2] = player;
}

2 个答案:

答案 0 :(得分:2)

因为第一个getchar()在输入流中留下了换行符,后续getchar()消耗了该换行符。一种方法是使用另一个getchar()来消耗不需要的换行符。

    printf("Player 1: Choose your symbol: \n");
    player1 = getchar();
    getchar(); // consume a newline

    printf("Player 2: Choose your symbol: \n");
    player2 = getchar();
    getchar(); // consume a newline

答案 1 :(得分:2)

你可能从getchar();

获得'\ n'字符

您可以执行以下操作:

printf("Player 2: Choose your symbol: \n");
player2 = getchar();
while ( player2 != '\n' ) { player2 = getchar(); }

在使用下一个getchar();

再次读取之前清除标准输入