From the course: Secure Coding in C
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Solution: Confirming input - C Tutorial
From the course: Secure Coding in C
Solution: Confirming input
(upbeat music) - [Instructor] This code doesn't do anything potentially dangerous with the input string, but it's still important to handle input properly. I've defined constants for the input buffer size here. A second constant sets the tax percentage rate, which is a floating point value. The first test is to ensure that a string was input by checking the value returned from the fgets function. When this value is null, the input string is null, and the program bails upon this condition. When a valid string is input, this while loop works as a filter. Valid characters are transferred from the original input string to an output buffer, one at a time. Along the way, the new line is removed, and then the loop is broken. Commas are skipped over here and the loop continues. Otherwise, any valid character is copied, and both indexes are updated. The next step is to translate the string into a floating point value. Variable r is used here to validate input. If the user types a string or…