Thread: how do I input only alphabets and spaces and no digits?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Delhi, India
    Posts
    6

    Exclamation how do I input only alphabets and spaces and no digits?

    I want to input people's names.....how do I input only alphabets and spaces and no digits?
    how do I do it??

    Also how do I see to it that the user enters something and doesen't leave the field blank.???

    Also How do I input only floats(salary)??

    HELP PLZZ...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you tried so far?

    We don't do homework here [and even if it's not technically homework, you probably need to learn how to solve simple problems yourself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since you can't physically stop people pressing the wrong keys, you have to read in a whole line (using say getline), then examine each character to make sure it fits the rules.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Salem, I agree with your suggestion, although for "fancy" applications, there is the other way around, where you read (without echo) the input, and filter out stuff that isn't supposed to be in there. That is not as easy, because it also means that you have to support all editing functions (e.g. backspace and delete).

    Edit: this also involved non-portable functionality, or using some library that supports non-echo mode and immediate input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by punk View Post
    how do I input only alphabets and spaces and no digits?
    how do I do it??
    How can I guarantee that people only give me what I want for Christmas?

    Also how do I see to it that the user enters something and doesen't leave the field blank.???
    Perhaps holding a gun to their head might work.

    Point is, you can't control what the user inputs, you can only validate and accept/reject.

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> Perhaps holding a gun to their head might work.
    Nah, I've tried it. They get all shakey and hit the wrong keys...
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed