gpt4 book ai didi

c - 加法练习程序中的错误

转载 作者:行者123 更新时间:2023-11-30 18:08:22 24 4
gpt4 key购买 nike

这是一个额外的练习程序,有一个我无法修复的错误。我无法在程序中输入Y/N字符,请帮忙我来修复它。

#include <conio.h>
#include <stdio.h>

main()
{
int answer, count;
int ch;
ch = getche();

for(count=1; count<11; count++) {
printf("What is %d + %d? ", count, count);
scanf("%d", &answer);
if(answer == count + count) printf("Right!\n");
else{
printf("Sorry, you're wrong\n");
printf("Would you like to try again? Y/N: \n");
scanf("%c", &ch);

if(ch=='Y') {
printf("\nWhat is %d + %d? ", count, count);
scanf("%d", &answer);
if(answer == count+count) printf("Right!\n");
else
printf("Wrong, the answer is %d\n", count+count);
}

else
printf("The answer is %d\n", count+count);
}
}
return 0;
}

最佳答案

scanf("%c", &ch); /* ch should be a char not an int ! */

另一个问题应该是 scanf("%c"):存在缓冲问题,之后必须清除输入缓冲区。

关于c - 加法练习程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3479681/

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