gpt4 book ai didi

c - 在C中使用fgets和sscanf在循环后搞乱了程序

转载 作者:行者123 更新时间:2023-11-30 15:46:50 28 4
gpt4 key购买 nike

我正在用 C 语言编写井字棋游戏。

一切似乎都很顺利,但我喜欢为这样的程序实现错误处理技术,以防用户输入错误数据。

我要求用户输入 1-9 之间的数字来填充井字图上的一个槽。当我一起使用 fgets 和 sscanf 时,它在第一场比赛中运行良好。然后,当用户选择“Y”或“y”继续玩新游戏时,似乎没有任何变量值刷新,这基本上导致了程序内的困惑。

有什么建议吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int printmatch(int array[3][3]);
int check(int array[3][3]);
char y;
char Y;
int complacer = 0;
int complacer2 = 0;

int main()
{
do
{

int fill = 0;
int j = 0;
int slot = 0;
int array[3][3];
array[0][0] = 0;
array[0][1] = 0;
array[0][2] = 0;
array[1][0] = 0;
array[2][0] = 0;
array[1][1] = 0;
array[2][1] = 0;
array[1][2] = 0;
array[2][2] = 0;
srand(time(NULL ));
printmatch(array);
char line[20];

do
{
do
{
tryagain: printf("\nEnter Position 1-9(from left to right):");

//I was using fgets and sscanf as an error handling technique incase user inputs incompatible data type, but after the 1st game is over, it seems this code messes up the functionality of the program
//fgets(line,sizeof(line),stdin);
//sscanf(line,"%d",&slot);

//when I just use scanf for data input, all is fine, but limited error handling
scanf("%d", &slot);
if (slot > 9 || slot < 1)
{
printf("Incorrect data input! Try again. \n");
}

} while (!(slot > 0 && slot < 10));

switch (slot)
{
case 1:
if (array[0][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][0] = 1;
check(array);
break;
case 2:
if (array[1][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][0] = 1;
check(array);
break;
case 3:
if (array[2][0] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][0] = 1;
check(array);
break;
case 4:
if (array[0][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][1] = 1;
check(array);
break;
case 5:
if (array[1][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][1] = 1;
check(array);
break;
case 6:
if (array[2][1] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][1] = 1;
check(array);
break;
case 7:
if (array[0][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[0][2] = 1;
check(array);
break;
case 8:
if (array[1][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[1][2] = 1;
check(array);
break;
case 9:
if (array[2][2] == -1)
{
printf("\nWoops, try again!");
goto tryagain;
}
array[2][2] = 1;
check(array);
break;

}

if (array[0][0] != 0 && array[0][1] != 0 && array[0][2] != 0
&& array[1][0] != 0 && array[2][0] != 0 && array[1][1] != 0
&& array[2][1] != 0 && array[1][2] != 0 && array[2][2] != 0)
{
check(array);

if (check(array) == 1)
{
printf("The user wins!\n");
}

else if (check(array) == -1)
{
printf("The computer wins.\n");
}

else
{
printmatch(array);
printf("It's a draw!\n");
}

goto done;
}
++fill;
label:

complacer = rand() % 3;
complacer2 = rand() % 3;

if (array[complacer][complacer2] == 0)
{
array[complacer][complacer2] = -1;
check(array);
}

else
goto label;

++fill;

printmatch(array);
int fullcheck = check(array);
if (fullcheck == 1)
{
printf("The user wins!");
break;
}

if (fullcheck == -1)
{
printf("The computer wins.");
break;
}

if (fill > 9)
break;
} while (fill < 10);
done: printf("\nDo you want to continue? Y/N\n");
scanf("%c %c", &y, &Y);
} while ((Y == 'Y' || Y == 'y'));
getchar();
return 0;

}
int printmatch(int array[3][3])
{

int i;
int j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\t", array[j][i]);
}

printf("\n");

}
}

int check(int array[3][3])
{
int settle;

if (array[0][0] == 1 && array[1][1] == 1 && array[2][2] == 1)
{
settle = 1;

}

else if (array[0][0] == -1 && array[1][1] == -1 && array[2][2] == -1)
{

settle = -1;

}

if (array[0][0] == 1 && array[0][1] == 1 && array[0][2] == 1)
{
settle = 1;

}
else if (array[0][0] == -1 && array[0][1] == -1 && array[0][2] == -1)
{

settle = -1;

}

if (array[0][2] == 1 && array[1][2] == 1 && array[2][2] == 1)
{
settle = 1;

}
else if (array[0][2] == -1 && array[1][2] == -1 && array[2][2] == -1)
{

settle = -1;

}

if (array[0][1] == 1 && array[1][1] == 1 && array[2][1] == 1)
{
settle = 1;

}
else if (array[0][1] == -1 && array[1][1] == -1 && array[2][1] == -1)
{

settle = -1;

}

if (array[1][0] == 1 && array[1][1] == 1 && array[1][2] == 1)
{
settle = 1;

}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;

}

if (array[0][0] == 1 && array[1][0] == 1 && array[2][0] == 1)
{
settle = 1;

}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;

}

if (array[2][0] == 1 && array[1][1] == 1 && array[0][2] == 1)
{
settle = 1;

}
else if (array[1][0] == -1 && array[1][1] == -1 && array[1][2] == -1)
{
settle = -1;

}
if (array[2][0] == 1 && array[2][1] == 1 && array[2][2] == 1)
{
settle = 1;

}

else if (array[2][0] == -1 && array[2][1] == -1 && array[2][2] == 1)
{
settle = -1;

}
return settle;
}

最佳答案

用户输入是邪恶的。当你想要一个数字时,有人会输入“A”。你想要一个“y”或“n”,然后你会得到几十个字母的输入。感谢您尝试通过防御性错误处理来改进您的编码。

使用sscanf()scanf()等时,一定要检查结果

int result;  
result = sscanf(buffer, "%this %that ...", &var1);
if (result != ExpectedResult) // handle error

fgets()getchar()scanf() 混合可能会混淆问题。建议不要将 fgets() 与这些一起使用 2. fgets() 是面向行的,scanf() 通常在使用换行符之前停止。

只输入“Y”或“y”继续很有趣,但使用“字母和 Enter_key”组合也不错,而且一开始更容易解决。

fgets() sscanf() 对可能导致了一些问题,但最终更容易解决。建议返回该样式并检查 sscanf() 的结果。

示例

scanf("%d", &slot);

在这里,您不知道用户在按“Enter”之前是否输入了任何数字。即使用户确实输入了数字,您的 scanf() 也会消耗前导空格和数字,但将“Enter”留给下一个输入函数。相反:

int ScanCount;
const char *prompt = "\nEnter Position 1-9(from left to right):";
do {
fputs(prompt, stdout);
prompt = "Incorrect data input! Try again. \n"; // For the maybe next time around
if (fgets(line, sizeof(line), stdin) == NULL) {
// Standard input is closed or some grievous I/O error, let's go home.
return 0;
}
ScanCount = sscanf(line,"%d",&slot);
} while ((ScanCount != 1) || (slot < 1) || slot > 9));
<小时/>

顺便说一句:这里有一个小技巧来测试是否在“9z”等数字后输入了额外文本。将 ScanCount = ... slot > 9)); 替换为

char c;
ScanCount = sscanf(line,"%d%[^\n]",&slot, &c);
} while ((ScanCount != 1) || (slot < 1) || slot > 9)); // still ScanCount != 1

关于c - 在C中使用fgets和sscanf在循环后搞乱了程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983035/

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