为什么在一种情况下需要清除缓冲区

问题描述

我编写了一个程序来学习 C。

#include <stdio.h>
#define LEN 10

int main(){

    char name[LEN],c=0;
    int counter=0,exit=0;
    printf("Enter your name: ");

    do{
        c = getchar();

        if(c=='\n')
            exit = 1;
        else{
            name[counter] = c;
            counter++;
        }
    } while(!exit && counter < LEN-1);
    name[counter] = 0;

    printf("You entered %s and counter is %d \n",name,counter);

    if(counter == LEN-1)
        while (getchar() != '\n');

    int choice;
    printf("Enter digit:");
    scanf("%d",&choice);
    printf("Your choice: %d \n",choice);

    return 0;
}

程序要求/读取一个字符串,然后要求/读取一个数字。

它对我有用... 我添加了消耗直到换行的循环以清除换行... 我可能没有研究得足够多,但有人可以解释为什么只有在用户输入填充数组的字符串(包括零)(?)时才需要这样做

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...