Link to home
Start Free TrialLog in
Avatar of sarvin rao
sarvin raoFlag for Malaysia

asked on

HOW DO I VALIDATE THE INPUT AND ASK USER TO VALIDATE IF WRONG

I had this question after viewing C++ Arithmetic operations on fractions - VERY URGENT.

Avatar of phoffric
phoffric

Please post your working code that is missing the  input validation. Have you thought about try/catch/throw?
http://www.cplusplus.com/doc/tutorial/exceptions/
There is some validation already being done in this code. What more are you looking for?
A well structured application should:
- Ask the user for data input.
- Validate data input, ask the user again of it is wrong or terminate the application, it all depend on your needs.
- Perform the action it is supposed to do.

That's at least 3 tasks, and they should appear as distinct functionalities in the source code, unlike the one you linked wich perform all at once (and I'm not talking about the other issues it has).

Side note:
@phoffric: C++ do not raise an exception when a divide by zero operation is performed, that's why data validation must be performed before executing a divide operation.
Plus, raising an exception in this case is not appropriate as this is a programming error.
@Fabrice,
The divide by zero Is already handled by the program in an overkill way.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.