Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using String-based Data Validation

Instead of relying on cin to validate input, read the input as a string and then check it. If the string passes the test, convert it to the desired type, say int or double.


advertisement
hat do a command shell, an online ordering system, and a report generator application have in common? Not much, really. Yet they all base their operation on an external source of data, be it a human user or a data file. By their nature, such applications must be prepared to detect invalid input and handle it in a reasonable manner.




The standard <iostream> library offers only a limited input-validation mechanism. How can you ensure that users don't accidentally enter a string as their zip code, for example? Although cin can detect such anomalous conditions, it has a cumbersome interface that doesn't provide sufficient information about the cause of the error. Worse yet, cin doesn't recover from errors automatically.



Instead of relying on cin to validate input, read the input as a string and then check it. If the string passes the test, convert it to the desired type, say int or double.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap