How to limit user input to a certain number of digits?

This is a discussion on How to limit user input to a certain number of digits? within the C Programming forums, part of the General Programming Boards category; I am writing a pretty simple program. There are a few different inputs I need to get from the user. ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    How to limit user input to a certain number of digits?

    I am writing a pretty simple program.

    There are a few different inputs I need to get from the user.

    1) Single character.

    I am using getchar() for this, and it works well, but I've noticed one thing: If the user enters a number instead, the program will crash. How do I fix this?

    2) Binary value

    The user types in a binary value ie: 10101010
    The problem is that I only want the user to be able to enter up to 8 bits. I am scanning it in as a string with scanf, then using strtol. As it is right now the user can enter more than 8 bits and the program will crash

    3) Decimal value

    Same problem as the binary value, I want to limit the user to inputting 3 digit decimal values.

    4) Hex value

    Same problem, I want to limit the user to entering 2 digit hex values.





    So basically my problem is limiting user input. I'm sure this is a relatively simple problem, but I have been searching for an answer and my head is starting to spin.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot actually limit the input with standard input functions. You will just need to set up a check and rule their input invalid if it doesn't fit what you need.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    You cannot actually limit the input with standard input functions. You will just need to set up a check and rule their input invalid if it doesn't fit what you need.


    Quzah.
    That means you cannot limit it in the sense that the user would be unable to type as much as they want no matter what, anyway.

    However, you don't have to accept all the data. For example:
    Code:
    char input[16];
    scanf("%15s",input);  /* leave room for '\0' */
    You can type as much as you want here, but only the first fifteen characters will be put into "input" (preventing the overflow that is causing your crash).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Ok for the most part, I have solved the problem of the user entering longer numbers than i wanted, but I still have a problem with the user entering the wrong type of data.

    If I ask for a decimal value and the user enters a character or a string, the program crashes, and vice versa.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    That means you cannot limit it in the sense that the user would be unable to type as much as they want no matter what, anyway.
    That's what I said.
    Quote Originally Posted by MK27 View Post
    However, you don't have to accept all the data.
    Yep. Said that too.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NewbGuy View Post
    Ok for the most part, I have solved the problem of the user entering longer numbers than i wanted, but I still have a problem with the user entering the wrong type of data.

    If I ask for a decimal value and the user enters a character or a string, the program crashes, and vice versa.
    The solution to this could be to use a loop for all the input, which collects a string into a temporary buffer (since a string can hold anything), and then you work on the string
    Code:
    #include <stdio.h>
    
    int main() {
    	char input[64], one;
    	int i, two;
    	for (i=1;i<=4;i++) {
                    switch (i) {
    			case (1): printf("Enter a character: "); break;
    			case (2): printf("Now a number: "); break; 
    ...etc
    		 fgets(input,64,stdin);
    		 switch (i) {
    			case (1): one=input[0]; break;
    			case (2): sscanf(input, "%3d", &two); break;
    ...etc
    With the second switch, you may want to implement error checking routines that will issue a message and set i-- if the user enters bad input.
    Last edited by MK27; 05-08-2009 at 10:23 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Can someone tell me why this doesn't work? It is supposed to simply get a character press from the user and if that character isn't equal to A, B or C, it should print "Error! Enter again!". When I run it, it does print the error message, but even if I hit A, B or C, it keeps telling me there's an error. I tried to take care of the input buffer so there's no problem with \n, so I'm stumped here.

    Also, should this be able to validate the entry so that if the user entered a number, it would give the same error as entering an incorrect letter?

    Code:
            char cType;
    
            cType = toupper(getchar());
    	fflush(stdin);
    	
    	while ( cTypeSelect != 'A' || cTypeSelect != 'B' || cTypeSelect != 'C')
    	{
    		printf("Error! Enter again! \n");
    		cTypeSelect = toupper(getchar());
    		fflush(stdin);
    	}
    Last edited by NewbGuy; 05-08-2009 at 10:28 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use &&, not II.

    By the way, the C standard recommends against using fflush() on stdin. Take both of those out and make this the first line in the while loop:
    Code:
    getchar();
    That will catch the \n.

    Finally, cType should be an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 07:10 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21