gpt4 book ai didi

c - 错误 : maybe the else if statement?

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:20 24 4
gpt4 key购买 nike

我已经有一段时间没有用 C 编写任何程序了,所以我需要一些帮助。下面的代码有某种错误。当我运行代码时,ifelse if 被跳过,屏幕上提示我的硬编码错误。

void play_tictactoe()
{
int row = 0, column = 0, i, win = 0;
char letter = 0;
char board[3][3] =
{
{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}
};

printf( "\nDo you want to be X or O: " );
scanf( "%c", &letter );

if( letter == 'x' || letter == 'X' )
{
for( i = 0; i > 9 && win == 0; i++ )
{
printf( "\nPlace X: Row 1-3 and Column 1-3 (i.e. 1,3 for Row 1 and Column 3): " );
scanf( "%i,%i", &row, &column );

do
{
playersTurnX( &row, &column, board );
computersTurnX( &row, &column, board );
}
while( row > 3 || column > 3);
}
}
else if( letter == 'o' || letter == 'O' )
{
for( i = 0; i > 9 && win == 0; i++ )
{
printf( "\nPlace O: Row 1-3 and Column 1-3 (i.e. 1,3 for Row 1 and Column 3): " );
scanf( "%i,%i", &row, &column );

do
{
//computersTurnO( &row, &column, &board );
//playersTurnO( &row, &column, &board );
}
while( row > 3 || column > 3);
}
}
else
{
printf( "\nError!!!!\tInvalid entry.\n" );
}
}

最佳答案

当您使用时:

scanf( "%c", &letter );

你最终甚至将换行符读入 c。使用:

scanf( " %c", &letter );
// ^^^^ add a space before %c.

使用 "%c" 将确保跳过所有空白字符,并且只有非空白字符将被读入 c

关于c - 错误 : maybe the else if statement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com