一、简单概述
输入缓冲区
所有从键盘输入的数据,不管是字符还是数字,都是先存储在内存的缓冲区中,叫作“键盘输入缓冲区”,简称“输入缓冲区”或“输入流”。
我们来设计一个程序:
设置密码,将密码保存到一个数组里,保存成功或失败都有提示。
按照思路,我们编写以下代码
void main()
{
int ch=0;
int a=0;
char sb[]={0};
printf("Please set your password:\n");
scanf("%s",sb);
printf("Are you sure?Y/N\n");
if ((a=getchar())=='Y')
printf("Password set successfully!");
else
printf("Password set failure!");
}
在visual c++2010版本下编译并运行,得到以下结果:
我们发现当输入完成密码后,应当执行判断语句并等待输入:
if ((a=getchar())&