C validating business rules
We already used scanf (in the code) to read user input.We'll use scanf here, but we'll also make sure that the item entered is actually a single-digit integer.
However, if you were expecting a valid integer, and then have to do any sort of math or logic around that number, C language would either spit out garbage or crash the program.Data can be entered incorrectly either by mistake or on purpose.C programmers must be vigilant to ensure both malicious and unintentional bad entry does not occur.That is, if you feed bogus information to a computer program, don't be surprised when you get garbage returned to you.As a programmer, you should continually strive to ensure that your programs only accept good information so that they can function properly.It is frustrating to have the program or menu simply quit and not allow the correct value(s) to be entered.
When providing user input, always give users an opportunity to enter the right information.
Plus, get practice tests, quizzes, and personalized coaching to help you succeed.
Try it risk-free You may have heard of the adage ''garbage in - garbage out'', or GIGO.
The following code makes sure that the user enters a 3-digit number.
The do...while loop is unique, in that the while condition just contains a 1.
We will make use of the ''strlen()'' function to check the length of the string.