c语言 中怎么用getchar()实现输入exit后就退出吖??

来源:百度知道 编辑:UC知道 时间:2024/05/15 23:21:58
#include<stdio.h>
void go()
{
int a,b,c,d,e;
long n,m;
printf("Please enter a number:");
scanf("%ld",&n);
a=n/10000;
b=n/1000-10*a;
c=n/100-n/1000*10;
d=n/10-n/100*10;
e=n%10;
if(a==0&&b==0&&c==0&&d==0&&e!=0)
{printf("The number has one number,it's %d\n\n",e);}
if(a==0&&b==0&&c==0&&d!=0)
{printf("The number has two number,it's %d,%d\n\n",d,e);}
if(a==0&&b==0&&c!=0)
{printf("The number has there number,it's %d,%d,%d\n\n",c,d,e);}
if(a==0&&b!=0)
{printf("The number has four number,it's %d,%d,%d,%d\n\n",b,c,d,e);}
if(a!=0)
{printf("The number has five number,it's %d,%d,%d,%d,%d\n\n",a,b,c,d,e);}
}
void main()
{
char c;
while((c=getchar())!='q')
{
go();
}}
========================================

1,getchar()等待键盘输入,无则一直等待。想直接执行go的话,用do{}while控制
do
{
go();
}while((c=getchar())!='q');

2,getchar()只能得到一个字符,不过方便,要用字符串的话,还得有字符串头文件,和比较函数strcmp();不用数组和指针是不行的(其实数组也是指针);
也不划算

getchar() 要等到按回车才会从键盘缓冲区读入。
你用getch()不回显或getche()有回显。(就是能看到你输入的字母)。不用按回车键。ok了。
void main()
{
char c;
while((c=getche())!='q')
{
go();
}
}