有没有办法限制 C 中的 scanf? [英] Is there a way of limiting scanf in C?

查看:58
本文介绍了有没有办法限制 C 中的 scanf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为链接列表的使用编写一个正确的控制台应用程序,因此我需要在无限循环中扫描多个命令并由于 switch case 选项执行某些操作.所以我为此使用了 scanf 但问题是当下一行不包含数字时它会循环并开始打印甚至不是默认值.

I am trying to write a correct console application for linked list usage, so i need to scan a number of a command in a infinite loop and do something due to switch case option. So i am using scanf for this but the problem is when the next line doesnt contain number it loops and starts printing not even default value.

`while(1)
    {
            printf("Enter a number of a command.\n");
            scanf("%d",&command);
            switch(command)
            {
            case -1:
            ....
            default:
                  printf("Reenter command.\n");
                  break;
            }
    }

似乎当我读取无限量的数据堆栈时会被重写.我知道我必须限制符号阅读的数量,但不明白如何以正确的方式做到这一点.

It seems like when i am reading the infinite amount of data stack gets rewrited. I know i have to limit the amount of symbols reading, but dont understand how to do this in right way.

在 Ubuntu 18.04.2 LTS 上使用 gcc 版本 5.4.0 (GCC)、c99

Using gcc version 5.4.0 (GCC), c99 on Ubuntu 18.04.2 LTS

推荐答案

我没有足够的声誉来发表评论,但这可能是您正在寻找的.还尝试更具描述性.我已经粘贴了您的代码,我发现的唯一问题是,当您按 Enter 键而不插入数字(即字母)时,它会跳过.这应该可以解决它:

I don't have enough reputation to comment, but this might be what you are looking for. Also try to be more descriptive. I have pasted your code and the only problem I could find is that when you press enter without inserting a number(i.e. a letter) it skips. This should fix it:

int readInt(const char *message, int min, int max){
    int num, control;
    do{
        printf("%s (%d a %d) :", message, min, max);
        control = scanf ("%d", &num);
        cleanBufferStdin();  
        if (control == 0)
        {
            printf("You should enter a number \n");
        }
        else{
            if(num<min || num>max)
            {
                printf("Number is invalid.\n");
            }
        }
    }  
    while(num<min || num>max || control ==0);

    return num;
}
void cleanBufferStdin(void)
{
    char chr;
    do
    {
        chr = getchar();
    }
    while (chr != '\n' && chr != EOF);
}

我编码了更多,并以另一种方式解释了您的问题(这个不仅检测您是否刚刚按下 Enter,而且如果您没有输入整数,我没有检查负数是否有效)我使用了此功能:

I coded for a bit more, and in another interpretation of your question(this one not only detects if you just pressed enter but if you didnt place an integer i didnt check if negative numbers work) I used this function:

//DONT FORGET TO #DEFINE WRONG_REQUEST_MACRO "SOME MESSAGE"
void readString(const char message*, char arrayChars *, int maxChars){
    int stringSize;
    unsigned char flag=0;
    do{
        flag =0;
        printf("%s", message);
        fgets(arrayChars, maxChars, stdin);
        
        
        stringSize = strlen(arrayChars);
        if (stringSize == 1){
            printf("[INFO]Empty request. You just pressed ENTER.\n"); 
            flag=1;
        }
        if (atoi(arrayChars)==0&&arrayChars[0]!=0){
            printf("[INFO]You didn't enter a number.\n");
            flag=1;

        }
    } while (flag == 1);

    if (arrayChars[stringSize - 1] != '\n'){
        clearBuffer(); 
    }else{
        arrayChars[stringSize - 1] = '\0'; 
    }

    while (strchr(arrayChars, '\'') != NULL || strchr(arrayChars, '?') != NULL || strchr(arrayChars, '*') != NULL || strchr(arrayChars, '\"') != NULL){
        printf("%s ' %s '", WRONG_REQUEST_MACRO, arrayChars);
        break;
    }
}

应该像这样使用

int command;
char message[20];//yes this could be used with a char pointer but lets assume op doesnt know how to allocate memory or work with char pointers it wouldn't change that much but if he does know how to do it he will promptly change

readString("something something i suppose\n",message,20); 
command=atoi(message);

尽管最后一个充满了调试重复"应该工作

Welp the last although its filled with debugging "duplicates" should work

这篇关于有没有办法限制 C 中的 scanf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆