| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1122 人关注过本帖
标题:接收字母输入的函数,非首次输入字母时需要输入两次
只看楼主 加入收藏
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:13 
接收字母输入的函数,非首次输入字母时需要输入两次
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
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:2 
&&(ch!='q'||ch!='Q') 是q或者Q就循环。所以输入Q/q就直接循环。也就是你说的无法接受他们。
ch!='q'&&ch!='Q'   要不是Q也不是q才循环。输入个M就循环,无法接受M但能接受Q / q.

梅尚程荀
马谭杨奚







                                                       
2012-01-09 09:55
ggyy4k
Rank: 5Rank: 5
等 级:职业侠客
帖 子:111
专家分:334
注 册:2010-6-28
收藏
得分:8 
两个问题:
1、在第二次以及其后输入字母的时候,不管输入其他什么都会提示“Please input A, B, C, D, Q or a, b, c, d, q.”,需要再次输入才能进行判断
我想这跟你读取输入函数get_first()有关,如果采用getchar()函数读入,内存缓冲区会有'\n'残留,必须采用下述语句消除:
while(ch=getchar()!='\n');
如果使用scanf()函数读入,必须要在每次读入后使用fflush()函数或者上述语句清除输入缓冲区。
2、语句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')
第一个表达式,(ch!='q'||ch!='Q')意思是当输入非'q'或者'Q'时求或,所以即使输入为'q'或者'Q',这个表达式值仍然为1。
第二个表达式,&&ch!='q'&&ch!='Q',输入为'q'或者'Q'时,表达式值为0。
所以这两个表达式是截然不同的。
2012-01-09 11:50
C_printf
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:102
专家分:122
注 册:2010-1-26
收藏
得分:2 
首先,你真正的读入字符的函数 没给出来....但是还是可以肯定你忘记了回车也是一个字符,也会被读取被判断,这个自己想办法解决。
while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q')) 这个如果输入Q,那么ch!=‘q’就成立了  ||后面的语句C语言就不再判断了 。。。
2012-01-09 15: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
C_printf
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:102
专家分:122
注 册:2010-1-26
收藏
得分:0 
....在pfan 论坛我已经回答了!
2012-01-10 09:38
gleerat
Rank: 2
等 级:论坛游民
帖 子:39
专家分:20
注 册:2009-12-23
收藏
得分:0 
回复 7楼 C_printf
关于缓冲区里面的换行符,我也知道需要消除,get_first函数也是仿照着教材上的实例来的,搞不出来为什么不生效。
我就是想不出来才问的,但凡自己可以解决,就绝不上论坛了,每次都是好几天之后才上论坛问都是实在无奈了。
自己糊里糊涂的,再思考也是白搭,还请你直接指明吧。

明察、慎思、笃行
2012-01-11 13:45
lonmaor
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
收藏
得分:3 
回复 8楼 gleerat
试试用fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃。

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2012-01-11 14:03
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:5 
程序代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>

int get_choice(void);

void main(void)
{
    int ch;
    do
    {
        ch = get_choice();
        printf("\n您的选择是:%c\n", ch);
    } while (ch != 'q');
    printf("\npress any key exit...");
    _getch();
}

int get_choice(void)
{
    const char choiceList[] = "abcdq";
    int ch;
   
    printf("\n");
    printf("A.add     \tB.subtract\n");
    printf("C.multiply\tD.divide\n");
    printf("Q.quit\n");
    printf("Please the operation of your choice:");
    do
    {
        ch = tolower(_getch());
    } while (!strchr(choiceList, ch));
    _putch(ch);

    return ch;
}

授人以渔,不授人以鱼。
2012-01-11 14:13
快速回复:接收字母输入的函数,非首次输入字母时需要输入两次
数据加载中...
 
   



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

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