标题:getchar()有清除缓冲区的功能?
只看楼主
michaelyj
Rank: 1
来 自:上海
等 级:新手上路
帖 子:55
专家分:0
注 册:2009-10-27
结帖率:88.24%
已结贴  问题点数:20 回复次数:2 
getchar()有清除缓冲区的功能?
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char ch;
    printf("please type in a letter: type # to end my act.\n");
    while((ch = getchar()) != '#')
    {
        if('\n' == ch)
            continue;
        if(islower(ch))
            switch(ch)
            {
                case 'a':
                    printf("argali\n");
                    break;
                case 'b':
                    printf("babirusa\n");
                    break;
                case 'c':
                    printf("coati\n");
                    break;
                case 'd':
                    printf("desman\n");
                    break;
                case 'e':
                    printf("echidna\n");
                    break;
                case 'f':
                    printf("fisher\n");
                    break;
                default:
                    printf("That's a stumper!\n");
            }
        else
            printf("I recognize only lowercase letters.\n");
        while(getchar() != '\n')
            continue;
        printf("Please type another letter or a #.\n");
    }
    printf("Bye!\n");
    getchar();
    getchar();
    return 0;
}

上面这个循环是不是就起到了这样的功能?
搜索更多相关主题的帖子: 缓冲区 getchar 
2010-03-16 11:27
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
得分:10 
getchar()每回重缓冲区取走一个字符,被取走的相当于被它吃掉了
while(getchar() != '\n')
            continue;   
上面代码的意思是从它取走的字符里面如果没有遇到回车符,就一直循环,也就是一直取

2010-03-16 11:38
陈大师
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:231
专家分:1038
注 册:2009-11-4
得分:10 
嗯~~~~getchar() 就是从缓存区取走一个字符,但是你没有把它赋给一个变量,相当于被吃掉了··
2010-03-16 12:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-299576-1-1.html




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

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