Thread: limiting length of users input

  1. #1
    Unregistered
    Guest

    Unhappy limiting length of users input

    Need some help from the experts...

    I want to limit how many characters a user can input when they are given a prompt

    e.g.

    Enter value =>234 ok
    enter value =>2344 prompts user that value entered is too long

    --------------------
    I know that I need to keep count of every character the user types, but I am trying to do it using cursor positions on the screen.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I suggest readin in the characters one at a time, and use some kind of counter to keep track of how many they entered in.
    Code:
    int readChars (char * aString, int aLength)
    {
     int i, overflow;
     overflow = 0;
    
     for (i = 0; i < length; i++)
    {
      aString[i] = getchar();
      if aString[i] == '\n' break;
    }
     i--;
    
     if (aString[i] != '\n') overflow = 1;
    
     aString[i] = '\0';
    
     return overflow;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Another way is this:

    char input[4];

    do{

    printf("/n/nEnter Input:/n/n");

    fgets(input,4, stdin);

    }while(strlen(input) > 3);// Or to ensure excactly 3 chars:
    //}while(strlen(input) != 3);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Unregistered
    Guest
    What I'm trying to do is have a space on screen, e.g. 2 cursor positions, that only the user type into. When they type over the allocated cursor positions an error is given.

    I have a place on screen where I would like the user to type the info I requested the inputs need to be integer and I can't get strlen to work on integers, I could convert it to a string, but I need to perform a calculation first before I convert. Its a form of error detection I'm trying to get going, i.e. no more than 3 digit integer entry, without using strlen, converting it to a string/characters and in real time (i.e. while the user is typing it in)

    below is a piece of code which shows the layout I'm trying to work on

    int num;
    int x,y;

    printf("\nEnter the 2 figues");

    textbackground(7);
    textcolor(0);
    x=wherex();
    y=wherey();
    cprintf("%c%c%c",32,32,32);
    gotoxy(x,y);
    cscanf("%d", &num);

    thanks

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    Lightbulb Is it really so easy?????

    Hi!

    Why donīt do this:
    Code:
    do
    {
      scanf("%d",&num);
    }while(num>999)
    When I close my eyes nobody can see me...

  6. #6
    Unregistered
    Guest
    Thats fine, but it requires pressing the return key after entering the number, I need an error to be shown while the users typing in the data, also your code would just overwrite the value held in num. But I know what you mean, thanks anyway :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. determine user's input
    By jp26198926 in forum C++ Programming
    Replies: 18
    Last Post: 05-11-2009, 06:15 PM
  2. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 04:02 AM
  3. Replies: 1
    Last Post: 07-10-2006, 06:30 AM
  4. troubleshooting user's input mistakes
    By jagerhans in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2001, 12:41 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM