Thread: c++ validation

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    c++ validation

    i have developed a program.lot of text and numeric inputs.


    input needs to be validated.


    1) All the validation is done right after we read it in by cinn>> ?

    2) There might be lot of numeric data to read. and if we each time type out the validateion for the string,then NO code-reuse. .if we create a validation class,and have validate Int,validate string,will that be correct?wrong?

    whats the best to design a program which requires lot of numeric and string input and needs to be validated.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rahulsk1947 View Post
    1) All the validation is done right after we read it in by cinn>> ?
    Normally, it's best to validate input as soon as it has been read. After the first wrong piece of input, there are likely to be further errors. Continuing after bad input is not only a waste of time, but possibly dangerous.

    2) There might be lot of numeric data to read. and if we each time type out the validateion for the string,then NO code-reuse. .if we create a validation class,and have validate Int,validate string,will that be correct?wrong?
    Validation usually is more about just the type of data. Sure, the input is an integer, but maybe it needs to be in the range of 0..10. A string might have to be all lower case. Etc. As the input constraints become more complicated, you are no longer doing just validation, but full-on parsing.

    whats the best to design a program which requires lot of numeric and string input and needs to be validated.
    It completely depends on your requirements, which haven't been specified.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just refactor as you go. If you have one place that does what you want to do in a second or third place, then make it its own function at that time.
    Any repeated four lines of code r more etc will do, just make it into a function and reuse it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    cin >> var will only read valid data for the datatype. It will flag an error and optionally throw an exception if the input data does not match what is expected of it.

    Beyond that, if you find yourself doing the same kind of parsing for different input lines you should write a function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It can also be very useful to use tables (perhaps using templates) to indicate various things. For example, an integer or a floating point value may have a min/max value. Strings may have a set of flags saying things like "must not have spaces", "only alpha chars", "Must be uppercase", "autoconvert to lowercase", and such things.

    This way, you don't have to write lots of different functions to do certain things.

    You may also want to write a function that takes a variable (string, integer/float) to be filled in, a set of flags or (min,max) values and a call-back function for validation.

    A call-back function will solve things like "this field must be a US state" (so checking that the value is one of "AZ", "IA", "TX", "CA" etc), whilst the automatic part will take care of the "convert to upper case".

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM