简体   繁体   English

在 C 中,如何只允许和取两位数作为输入?

[英]How to allow & take only two digit numbers as Input, in C?

I'm currently doing a project in which I've to take & consider only two digit numbers which an user is entering.我目前正在做一个项目,在该项目中我必须只考虑用户输入的两位数字。 I don't want to take an input & then check whether it's LESS THAN 100 or not.我不想输入然后检查它是否小于 100。

Instead of doing this checking, is there any other way of filter out only 2-digit numbers & accept it as input, while programming in C-language?除了进行这种检查之外,还有没有其他方法可以在用 C 语言编程时仅过滤掉 2 位数字并将其作为输入接受? As, I don't even want to allow the user to enter 3 digit number.因为,我什至不想让用户输入 3 位数字。 For example, if the user wants to enter 123, he won't be able to do it.例如,如果用户想输入123,他将无法做到。 Only if he is entering 12 or 23, then only the input is accepted.只有当他输入 12 或 23 时,才接受输入。

Thank you.谢谢你。

We could truncate the input to 2 digits but that is NOT what the question asked.我们可以将输入截断为 2 位数字,但这不是问题所问的。 It said inputs of more than 2 digits are to be ignored.它说超过 2 位数的输入将被忽略。

Additionally, the problem with simply restricting the number of digits input, is that any truncated digits are taken by the next input.此外,简单地限制输入的数字数量的问题是任何被截断的数字都会被下一个输入采用。 So for example with所以例如

scanf("%2d",&myNum);

if I enter 678 the 8 remains in the buffer and is read for the next input.如果我输入6788保留在缓冲区中并为下一个输入读取。 We could clear the input buffer but that is not what the question asked: you don't want numbers truncated to 2 digits, you want them ignored.我们可以清除输入缓冲区,但这不是问题所问的问题:您不希望数字被截断为 2 位数字,而是希望它们被忽略。

Here is my answer, which reads the digits as a text string and rejects those which do not have 2 digits, and those that are not digits.这是我的答案,它将数字读取为文本字符串并拒绝那些没有 2 位数字的数字和那些不是数字的数字。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    int myNum;
    int c;
    char str[4];

    do {
        myNum = 0;
        printf("Enter number: ");
        scanf("%3s",str);
        while((c = getchar()) != '\n' && c != EOF);   // flush the input
        if (strlen(str) != 2 || !isdigit(str[0]) || !isdigit(str[1]))
            continue;
        if (sscanf(str, "%d", &myNum) != 1)
            continue;
        printf("You entered %d\n\n", myNum);
    } while (myNum != 10);
    return 0;
}

Sample session:示例会话:

Enter number: 42
You entered 42

Enter number: 678
Enter number: 1
Enter number: 10
You entered 10

You can see thet 678 and 1 were not accepted.您可以看到6781未被接受。

You need to limit digits while having scanf .您需要在使用scanf限制数字。 I hope below code snippet will work for you.我希望下面的代码片段对你有用。

int main()
{
    int myNum;
    printf("Enter number");
    scanf("%2d",&myNum);      // here I am taking input of two digits only
    printf("%d", myNum);      // Verifying input by printing 
}

I hope for single digits you will input as 01 or 1 , still it will work out same.我希望您输入的个位数为011 ,但结果仍然相同。

SOLUTION:解决方案:

scanf("%2d",&variableName);

i think it'll work....我认为它会起作用......

If you are looking for a program that disables the alphabet input and restrict numeric input only to two digits, I guess that needs some kind of interfacing with keyboard(with terminal I/O settings) which is slightly harder.如果您正在寻找禁用字母输入并将数字输入限制为两位数字的程序,我想这需要某种与键盘(带有终端 I/O 设置)的接口,这稍微困难一些。 Other than that I guess there are two simpler ways to do so in addition to scanf() :除此之外,我想除了 scanf() 之外还有两种更简单的方法:

1) Use a character array to let the user enter the number, but you flush out all the extraneous digits except first two and feed the array to atoi() to make it real integer value for further use in your program. 1) 使用字符数组让用户输入数字,但您清除除前两位以外的所有无关数字,并将数组提供给 atoi() 以使其成为真正的整数值,以便在您的程序中进一步使用。

2) In order to avoid user to scribble the console, you can use ncurses facility which provides getche() like use. 2) 为了避免用户在控制台上乱写,您可以使用提供 getche() 之类的 ncurses 工具。 Refer What is Equivalent to getch() & getche() in Linux?请参阅Linux 中的 getch() 和 getche() 等效于什么?

Finally after taking two characters you should be able to convert them as integer and use.最后,在取两个字符后,您应该能够将它们转换为整数并使用。

In both the approaches you can validate if user has actually entered numerics, and then convert them to integers.在这两种方法中,您都可以验证用户是否实际输入了数字,然后将它们转换为整数。

#include<stdio.h>   
#define CHOICE 2   
int digit(int num)  
{  
    int i=0;  
    while(num>0)  
    {  
        num=num/10;  
        ++i;  
    }  
    return i;  
}  
int main()  
{  
    int num=0;  
    do  
    {  
       printf("\nInput two digit number: ");  
       scanf("%d",&num);  
       if(digit(num)==CHOICE)  
       {  
          break;  
       }  
       else  
       {  
           printf("\nInvalid Input");  
           continue;  
       }  
    }  
    while(1);  
    printf("You entered %d",num);  
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM