C语言--scanf

关于C语言的scanf,首先看个例子

int get_int(void){
        int input;
        char ch;
        while(scanf("%d",&input)!=1){
                printf("is not an integer,please enter agin\n");
        }
        printf("%d\n",input);
        return input;
}

这个例子中,如果你输入的不是一个数字的话,程序就会陷入死循环,原因: 如果scanf没有成功读取输入就会将其留在输入队列中,所以下次再从输入队列中读取的时候,还是失败,所以循环了

改正:

int get_int(void){
        int input;
        char ch;
        while(scanf("%d",&input)!=1){
                while(getchar()!='\n'){
                        continue;
                }
                printf("is not an integer,please enter agin\n");
        }
        printf("%d\n",input);
        return input;
}

从输入队列中剔除那些有问题的输入

 

例子:

/**
 * menu.c
 * @desc 菜单技术
 */
#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);

int main(void){
        char choice;
        choice = get_choice();
        if(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");
}

/**
 * 获取一个整数型的输入
 */
int get_int(void){
        int input;
        printf("Please input a int:\n");
        while(scanf("%d",&input) != 1){
                printf("input error,Please input again:\n");
                while(getchar() != '\n'){
                        continue;
                }
        }
        return input;
}

/**
 * 获取用户输入字符串的第一个字符
 */
char get_first(void){
        char ch;
        ch = getchar();
        //清空输入队列中的其他字符
        while(getchar() != '\n'){
                continue;
        }
        return ch;
}

char get_choice(void){
        char ch;
        printf("Enter the letter if your choice:\n");
        printf("a. advice                       b. bell\n");
        printf("c. count                        q. quit\n");
        ch = get_first();
        while((ch < 'a' || ch > 'c') && ch != 'q'){
                printf("Please choice a b c or q\n");
                ch = get_first();
        }
        return ch;
}

void count(void){
        int n,i;
        printf("Count how far? Enter a integer:\n");
        n=get_int();
        for(i=1;i<=n;i++){
                printf("%d\n",i);
        }
        while(getchar() != '\n'){
                continue;
        }
}

 

转载于:https://www.cnblogs.com/bai-jimmy/p/4402406.html

引用中提到了scanf_s函数的使用方法和格式控制字符串的注意事项。在使用scanf_s函数读取输入时,除了格式声明之外,如果格式控制字符串中还包含其他字符,那么在输入数据时需要在对应位置上输入与这些字符相同的字符。例如,如果格式控制字符串中包含逗号分隔符,那么输入数据时需要按照逗号分隔的格式输入数字。但是如果在格式控制字符串中只使用了空格作为分隔符,那么输入数据时可以使用空格或其他分隔符进行区分。但是在上面的例子中,只能使用逗号作为分隔符,不能使用其他格式。中提到了在使用scanf函数读取字符串时的一个问题。当使用%s格式读取字符串时,遇到空格就会认为字符串结束,无法读取包含空格的完整行。这个问题可以通过使用其他的输入函数或者使用fgets函数来解决。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [C语言使用scanf_s函数输入的正确姿势](https://blog.youkuaiyun.com/abtain/article/details/80101878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [C语言笔记-考研版(进阶)](https://download.youkuaiyun.com/download/qq_51089445/87793919)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值