读书人

不是首次输入的话需要两次才能正确响

发布时间: 2012-03-24 14:00:46 作者: rapoo

不是首次输入的话,需要两次才能正确响应
get_choice函数要求输入字母A、B、C、D、Q或者a、b、c、d、q,其他均为非法输入,返回字母a、b、c、d、q。
但是,在第二次以及其后输入字母的时候,不管输入其他什么都会提示“Please input A, B, C, D, Q or a, b, c, d, q.”,需要再次输入才能进行判断;
get_first函数是教材上的一个实例,在该示例中是可以略过回车的,在这里就不行了。

C/C++ code
int get_choice(void) {     int ch;         printf("Please the operation of your choice:\n");    printf("A.add     \tB.subtract\n");    printf("C.multiply\tD.divide\n");    printf("Q.quit\n");    ch=get_first();    while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q'){        printf("Please input A, B, C, D, Q or a, b, c, d, q.\n");        //不是首次输入的话,任何输入都需要输入两次        ch=get_first();    }    if((ch>='A'&&ch<='D')||ch=='Q')        ch+=32;             return ch;}int get_first(void){     int ch;    ch=getchar();    while(getchar()!='\n')        continue;    return ch;}
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
a
Please input a float number: 2
Please input a float number: 3
2.0 + 3.0 = 5.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
b
Please input A, B, C, D, Q or a, b, c, d, q.
b
Please input a float number: 6
Please input a float number: 4
6.0 - 4.0 = 2.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
c
Please input A, B, C, D, Q or a, b, c, d, q.
c
Please input a float number: 3
Please input a float number: 0
3.0 * 0.0 = 0.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
d
Please input A, B, C, D, Q or a, b, c, d, q.
d
Please input a float number: 6
Please input a float number: 8
6.0 / 8.0 = 0.8.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
q
Please input A, B, C, D, Q or a, b, c, d, q.
q
还有,在教材上的那个实例中,get_first函数中如果把ch=getchar();放到while语句中写成while((ch=getchar())!='\n')就不能正确响应,请问为什么。
附教材实例:
C/C++ code
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  *实例8.8,混合输入字符和数值;菜单技术,使用switch分支语句  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */#include <stdio.h>char get_choice(void);char get_first(void);char get_first(void);void count(void);int main(void){    int choice;        while((choice=get_choice())!='q'){        switch(choice){            case 'a':printf("Buy low, sell high.\n");                break;            case 'b':putchar('\a');                break;            case 'c':count();                break;            default :printf("Program error!\n");                break;        }    }    printf("Bye!\n");        return 0;}char get_choice(void){    int ch;        printf("Enter the letter of your choice:\n");    printf("a.advice\tb.bell\n");    printf("c.count \tq.quit\n");    ch=get_first();    while((ch<'a'||ch>'c')&&ch!='q'){        printf("Please input a, b, c or q: \n");        ch=get_first();    }        return ch;}char get_first(void){    int ch;        ch=getchar();    while(getchar()!='\n')        continue;        return ch;}char get_int(void){    int input;    char ch;        while(scanf("%d",&input)!=1){        while((ch=getchar())!='\n')            putchar(ch);        printf(" is not an integer, please enter agin.\n");    }        return input;}void count(void){    int n,i;        printf("Count how for? Enter an integer: \n");    n=get_int();    for(i=1;i<=n;i++)        printf("%d\n",i);    while(getchar()!='\n')        continue;} 



[解决办法]
get_first用fflush(stdin);清空下缓冲区试试
[解决办法]
C程序不就是一步一步执行么, while(ch=getchar)和ch=getchar,while(getchar)能是一个效果么?

还有你考虑过直接敲一个回车的情况下ch是什么吗? 自己多思考思考。
[解决办法]
探讨

C程序不就是一步一步执行么, while(ch=getchar)和ch=getchar,while(getchar)能是一个效果么?

还有你考虑过直接敲一个回车的情况下ch是什么吗? 自己多思考思考。

[解决办法]
你要么就按照规则来输入,要么就把自己判断项加全,这样都不会出问题。。。注意缓冲区的问题
[解决办法]
int get_first(void)
{
int ch;

while('\n' == (ch=getchar()));//上面的回车接受掉。这样就OK了...

while(getchar()!='\n')
continue;

return ch;
}

读书人网 >C语言

热点推荐