Input/Validation

This is a discussion on Input/Validation within the C++ Programming forums, part of the General Programming Boards category; How to validate for a string to not contain any numerals at command prompt. I heard of using cin.get but ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Input/Validation

    How to validate for a string to not contain any numerals at command prompt.

    I heard of using cin.get but is not working for me.......using Turbo c++

    can anybody help

    regards

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    read the input as you normally would (into a std::string-instance), then use std::string::find_first_not_of, passing a string with all valid characters . If the function doesn't returns std::string::npos you got something you don't want in your input.

    Or you could use std::string::find_first_of to check if the string contains any "invalid" characters (which you have passed as an argument to the function).
    Last edited by edoceo; 03-23-2009 at 09:42 AM. Reason: typo

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    I need that on every character entered it checks whether it's numeral or not....if not then do not accept..


    regards

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    > I need that on every character entered it checks whether it's numeral or not....if not then do not accept..

    if you want the input to be stored inside a std::string, read my post above.

    Otherwise check the returned value from std::cin to see if there was any errors while reading from that stream.
    If you are trying to read data into a int/double/float/char/what/ever and the user inputs something other then what is expected (std::cin >> x) will be equal to 0.

Popular pages Recent additions subscribe to a feed

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