Thread: How to fix length of user input

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    How to fix length of user input

    I wrote program that can print number according to user input How to fix length of user input.I want to fix length of user input. I want to write program where user can print only any four numbers. How to fix length of user input ?

    Code:
    #include<stdio.h>
    int main (void)
    {
      int i;
      printf("user input");
      scanf ("%d",&i);
      printf ("Number's %d \n", i);    
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to stick to using standard I/O facilities, then you cannot control what the user enters, but you can check whether what they entered conforms to your requirements, and if it doesn't, you can reject their input and request for new input that conforms to your requirements. This can be placed into a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    isdigit
    if statements
    loops // as suggested
    for a number how big the number vs more then one set of number(s) for input?
    ie. 9 is one digit number whereas 10 is a two digit number, and 100 is a three digit number. etc. so the length of input to check against would depend on what input you are seeking, just check to make sure it matches if not, discard their first reply and ask again. ie loop

    or go sloppy, and just use the first chunk of input (amount you actually want) for your operation on it, and the rest just gets left out no matter what. check that if it is invalid loop around and have them try it again.
    Code:
    while (not what you want)
    {
    
    ask question, 
    get input
    
    }
    
    do more stuff ....
    Last edited by userxbw; 10-09-2017 at 03:51 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    fgets() and sscanf() are a pretty standard approach as well. Use a format like "%4d%c", and if you get two conversions and the character is an \n, then the input is good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-26-2013, 02:48 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. Replies: 1
    Last Post: 07-10-2006, 06:30 AM
  4. length of input number
    By Alabsi in forum C++ Programming
    Replies: 9
    Last Post: 06-25-2003, 10:55 AM
  5. limiting length of users input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-20-2001, 09:55 AM

Tags for this Thread