高手帮忙看一下这个while循环第二次以后怎么运行不对了?

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:46:09
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 50
void info(void);
void userinfo(void);
void converTolowercase(char *array);
void searching(char string[SIZE]);
int main()
{

char str='Y';
info();
while(str =='y'||str =='Y')
{
userinfo();
printf ("Input 'Y' or 'y' to continue\n");
printf ("Input other characters to exit\n");
getchar();
str=getchar();
printf ("*************************************************************\n");
}
printf ("Thank you for using the system !\n");/*程序退出*/
printf ("Press enter button to exit!\n");

getchar();
getchar();

return 0;
}
void info (void)/*系统提示信息以及版本说明*/
{

printf ("***************

在C语言中,接受输入单个字符的函数如scanf、getchar()等都有一个毛病,就是程序接受完用户输入的字符后并没有清空键盘输入缓冲区(或者说标准输入流),因此当用户输完字符和回车后,回车字符还留在标准输入流中,而gets函数又恰好从缓冲区的当前字符开始接收输入,这样,它接收的到第一个字符就是上次输入遗留下来的回车符号,于是程序又会把它作为结束输入的标志,不再接受用户的键盘输入。
故在gets(string); 前加一句fflush(stdin); 这句的作用就是清空当前的标准输入流(即键盘缓冲区。)