【发布时间】:2023-04-04 11:04:01
【问题描述】:
此代码不像在 CodeBlocks 中那样工作:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
char choice;
do
{
printf("Enter a character using getchar(): ");
fflush(stdin);
c = getchar();
printf("The entered charcter is: ");
putchar(c);
do
{
printf("\nDo you want to continue [Y/N]: ");
fflush(stdin);
choice = getchar();
if (choice >= 'a' && choice <= 'z')
choice -= 32;
} while (choice != 'Y' && choice != 'N');
printf("\n");
} while (choice == 'Y');
system("pause");
return 0;
}
输出是:
Enter a character using getchar(): !
The entered character is: !
Do you want to continue [Y/N]:
Do you want to continue [Y/N]: y
Enter a character using getchar(): The entered character is:
Do you want to continue [Y/N]:
为什么 fflush(stdin) 不起作用?
有没有其他办法解决这个问题?
【问题讨论】:
标签: c visual-studio