Error correction for data types - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Error correction for data types

QubikQubik Posts: 10Member
I am doing a small research on error correction for wrong data types and would like your contributions if any. What would you do to prevent your program from crashing when the user enters an alphabetic character instead of an integer.(more to come later)

Qubik

Comments

  • PerranPerran Posts: 241Member
    : I am doing a small research on error correction for wrong data types and would like your contributions if any. What would you do to prevent your program from crashing when the user enters an alphabetic character instead of an integer.(more to come later)
    :
    : Qubik
    :
    This has been covered several times on this board. ;-) The best thing to do [IMHO] is to PREVENT the user from entering the wrong type of data. This can be done by capturing individual keystrokes as CHARs and testing them for propriety BEFORE submitting them to processing routines. The easiest way to test them for propriety is use the "in" operator and have a set of proper keys. e.g. for numbers your set would be ['0'..'9', #08, #13] allowing for backspace and enter. So, you capture individual keystrokes and then construct a string AFTER testing for set membership. HTH
  • ManningManning Posts: 1,621Member
    : I am doing a small research on error correction for wrong data types and would like your contributions if any. What would you do to prevent your program from crashing when the user enters an alphabetic character instead of an integer.(more to come later)

    Like Perran, I prefer to just stop them from entering invalid data. A common thing for people new to the language is to use ReadLn() for everything. That's easy, but not safe. So instead I have a couple dozen input routines that have various uses. For example inputting a number that falls in between two values, a string that is a certain length or fits a certain pattern, etc..

    In the end this way is more work, but for me always knowing the input is valid outweighs the extra coding required (especially since the routines are all generic and re-usable, so they only need to be written once).
Sign In or Register to comment.