limiting length of users input

This is a discussion on limiting length of users input within the C Programming forums, part of the General Programming Boards category; Need some help from the experts... I want to limit how many characters a user can input when they are ...

  1. #1

    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


    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
    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.
    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;
     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
    Waterloo, Texas
    Another way is this:

    char input[4];


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

    fgets(input,4, stdin);

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

  4. #4
    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");

    cscanf("%d", &num);


  5. #5
    Registered User
    Join Date
    Oct 2001

    Lightbulb Is it really so easy?????


    Why donīt do this:
    When I close my eyes nobody can see me...

  6. #6
    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, 03: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, 11:41 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

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