gpt4 book ai didi

c - C中的scanf字符

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

<分区>

出于某种原因,scanf 读取了该字符,但即使该字符有效,程序也不会继续通过用于验证该字符的 while 循环。有时有效,有时无效。奇怪的是我没有做任何不同的事情。有什么想法吗?

do
{
printf("Enter \"p\" if you want to sort and shuffle a list of players or enter \"s\" if you want to sort a list of slots: "); // prompt
scanf("%c", &tmp);
} while (tmp != 'p' && tmp != 's');

新代码:

printf("Enter 'p' if you want to sort and shuffle a list of players or enter 's' if you want to sort a list of slots:"); // prompt
tmp = getchar();

printf("%c ",tmp);
if (tmp == 'p')
{
size = readPlayerFile(players); // calling readPlayerFile function
shufflePlayers(players, size); // call shufflePlayers function
sortPlayers(players, size); // call sortPlayers function
}
else if (tmp == 's')
{
printf("hello");
size = readSlotFile(slots); // calling readSlotFile function
sortSlot(slots, size); // call sortSlots function

}

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