| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1122 人关注过本帖
标题:接收字母输入的函数,非首次输入字母时需要输入两次
取消只看楼主 加入收藏
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
接收字母输入的函数,非首次输入字母时需要输入两次
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.”,需要再次输入才能进行判断;
程序代码:
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

还有,语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q'))跟语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q')在逻辑上不是一样的么?怎么前者就无法接受Q和q呢?

[ 本帖最后由 gleerat 于 2012-1-9 17:45 编辑 ]
搜索更多相关主题的帖子: 字母 函数 
2012-01-09 09:22
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 3楼 ggyy4k
这是其中涉及到get_first的函数,这个函数在其他程序中没有出现问题,也是教材上的一个实例中使用的。
程序代码:
int get_first(void)
{
    int ch;

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

    return ch;
}
但是在这个程序中就无法正常进行


明察、慎思、笃行
2012-01-09 17:29
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 4楼 C_printf
这是其中涉及到get_first的函数,这个函数在其他程序中没有出现问题,也是教材上的一个实例中使用的。
程序代码:
int get_first(void)
{
    int ch;

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

    return ch;
}
但是在这个程序中就无法正常进行。

[ 本帖最后由 gleerat 于 2012-1-9 17:31 编辑 ]

明察、慎思、笃行
2012-01-09 17:30
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 7楼 C_printf
关于缓冲区里面的换行符,我也知道需要消除,get_first函数也是仿照着教材上的实例来的,搞不出来为什么不生效。
我就是想不出来才问的,但凡自己可以解决,就绝不上论坛了,每次都是好几天之后才上论坛问都是实在无奈了。
自己糊里糊涂的,再思考也是白搭,还请你直接指明吧。

明察、慎思、笃行
2012-01-11 13:45
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 3楼 ggyy4k
原来还不了解你的帖子中第一点是什么意思,后来别人给支招之后才知道你的意思。
谢谢。
程序代码:
char get_first(void)
{
    int ch;
    
    while((ch=getchar())=='\n')
        ;//略过上一步驻留在缓冲区中的回车符
    while(getchar()!='\n')
        continue;
    
    return ch;
}

明察、慎思、笃行
2012-01-11 16:56
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 10楼 TonyDeng
谢谢,你的代码不错,不过我只是想在自己的代码上面改的。

明察、慎思、笃行
2012-01-11 16:58
快速回复:接收字母输入的函数,非首次输入字母时需要输入两次
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016084 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved