-1

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.

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? As, I don't even want to allow the user to enter 3 digit number. For example, if the user wants to enter 123, he won't be able to do it. Only if he is entering 12 or 23, then only the input is accepted.

Thank you.

Ashish Gope
  • 320
  • 1
  • 5
  • 13
  • 1
    What about single-digit numbers? – Weather Vane Jul 25 '15 at 18:57
  • For reading a 2-digit number, `%3d` would be needed unless `+99` (99) and `-99` aren't valid entries. –  Jul 25 '15 at 19:24
  • 1
    If you explain why you want to leaving unread input characters, rather than reading a number and then bounds-checking, this would help people give you reasonable suggestions. If you want to reject `123`, you need to read all the characters, or else they'll still be there in the input stream. You really do need a `do { scanf } while(invalid && !io_error)` loop. You can't have scanf reject something and get new input for you. It's too low-level an API for that. – Peter Cordes Jul 25 '15 at 19:50
  • Well, @PeterCordes **thanks for your suggestion**. Pardon my ambiguous description. But the thing I wanted to say is, **I don't want to allow the user to input a number, more than 2-digits.** The user will've to enter only 2-digit number. Is this possible at all? \ – Ashish Gope Jul 25 '15 at 23:53
  • But what exactly do you want to happen if they try? If you never read the character from the input stream, it will still be there when you try to read more. So your program will be stuck. You know simple Unix / C terminal input is line-buffered, right? So you can't reject a character as it's typed, you have to wait for the user to press return, read the line, and then do something. If you leave some of the line unread, it will still be there next time. – Peter Cordes Jul 26 '15 at 00:09
  • Yes, you're right, that the unread portion will get read next time when I read. But then, they will enter a 3 digit number & then I should determine & chose only the first two digit. Is this how the problem will end? As @Ripunjay mentioned below, I've tried it already. But I wanted to restrict the user from entering more than 2 digit numbers. But according to your answer, it seems a improper idea to restrict the user. :/ – Ashish Gope Jul 26 '15 at 00:36

5 Answers5

4

We could truncate the input to 2 digits but that is NOT what the question asked. It said inputs of more than 2 digits are to be ignored.

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. 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.

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.

#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.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • I've tried your approach already. But my problem is to how to restrict the user with only 2digit inputs, so that he won't be able to input more than 2 digit numbers. I don't want to ignore or truncate any of the inputs. I just don't want the user to be able to enter more than 2 digit number, anyhow. Hope Now i've clearly mentioned my problem. – Ashish Gope Jul 26 '15 at 00:08
  • @AshishGope no you really have not stated a clear problem. I enter 2 digits, if that satisfies your input, why should you be concerned about any 3rd digit? I don't get it, why does your mission statement say, hey, lets see if he enters another digit? If you had posted some code showing what you are doing, and where the problem is, that would have been more credible, but your requirement is vague, and *"I don't want to take an input & then check whether it's LESS THAN 100 or not."* is ridiculous. – Weather Vane Jul 26 '15 at 22:12
  • Dear @Weather Vane, I was looking for an mere idea of how to restrict an user to enter only 2 digits number. What I meant to say is, is this really possible in C? If this would 've been possible, then the concept of truncating/ignoring the extra digits, could 've been ignored. How come you say that my requirement is vague? Seeking help regarding a problem & learning new concepts are never a vague idea to me. May be my question or idea is not suitable for your cup, but it's not a vague & ridiculous idea at all. Thank you for suggestions, it was a great help. thanks a lot. – Ashish Gope Jul 27 '15 at 14:44
0

You need to limit digits while having 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.

Amol Saindane
  • 1,568
  • 10
  • 19
0

SOLUTION:

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

i think it'll work....

RatikBhat
  • 61
  • 11
  • 1
    No bro it didn't. It takes watever u're feeding into it. When printing then only it prints the 2-digit number. My problem is how to take only 2 digit number, i.e. restrict the user to enter more than 2 digit number. – Ashish Gope Jul 26 '15 at 00:03
0

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. Other than that I guess there are two simpler ways to do so in addition to 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.

2) In order to avoid user to scribble the console, you can use ncurses facility which provides getche() like use. Refer What is Equivalent to getch() & getche() in Linux?

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.

Community
  • 1
  • 1
ultimate cause
  • 2,264
  • 4
  • 27
  • 44
  • I think the above part of your reply makes sense, but how to do that interfacing? & talking about the option number 1 & 2 it's not solving the problem. The user is still able to enter 3 digit values. – Ashish Gope Jul 26 '15 at 00:16
  • @Ashish Gope : 1. Have a loop which takes a character with getch() as said in the link I pasted. 2. If the input character is an alphabet or a white space, do not echo it on screen and reject it. 3. If you have got two digits break the loop. This will solve all your cases I hope. – ultimate cause Jul 26 '15 at 02:01
0
#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);  
}  
Shivam Seth
  • 677
  • 1
  • 8
  • 21