Code Responsibly-- How Can I Properly Validate nput? heck all input: Below is a
ID: 3703176 • Letter: C
Question
Code Responsibly-- How Can I Properly Validate nput? heck all input: Below is a partial list of some checks to include: . Type check input should be checked to ensure it is the datatype expected, e.g., age must be integer . Length check: variables are checked to ensure they are the appropriate length, for example, a US telephone number has 10 digits. Range check-numbers checked to ensure they are within a range of possible values, for example, the value for month should lie between 1 and 12 Reasonable check: values are checked for their reasonableness, e.g. (age> 16) && (age 100) Divide by Zero: variables are checked for values that might cause problems such as division by zero. .Format check - Checks that the data is in a specified format (template), e.g, dates have to be in the format DD/MM/YYYY Input checking can be difficult. Checking a telephone number may require consideration of the many differing telephone formats used by countries around the world. Recover Appropriately A robust program will respond to invalid input in a manner that is appropriate correct, and secure. When your program runs across invalid input, it should recover as much as possible and then repeat the request, or otherwise continue on. Arbitrary decisions such as truncating or otherwise reformatting data to "make it fit" should be avoided. The following method shows input validation to check range and/or reasonableness: in ValidNum(int min, ?nt max) int value; sin value; while ((value max)) coutExplanation / Answer
Question 1: Which of the following inputs should be checked for reasonableness?
Age first name city item Price
Answer: “Age” should be checked as this input in real life can not go beyond a specific range. For example, a human being can not have age in negative numbers or in general age beyond 100 years.
Reasonableness check is checking the input, which can be possible in reality. Another example can be the number of months in a year can not go beyond 12 or less than 1.
Question 2: Which of the following inputs would require a specific template or format to be appropriate?
Social security number national debt Telephone number birthdate
Answer: “Birthdate” should be checked for a specific format because a date can be defined in different formats for different people such as some use DD/MM/YYYY or MM/DD/YYYY or YYYY/DD/MM or any other way. Which can cause ambiguity as if as input reads the date as DD/MM/YYYY and user puts date the other way, then date will not be correct.
Question 3: Complete the below security checklist for the program below:
#include <iostream>
using namespace std;
bool checkHeight(int H)
{
if (H>21.51 && H<107.08) //The range of height of average adult human being is 21.51 inches to 107.08 inches
{
return true;
}
return false;
}
bool checkWeight(int W)
{
if (W>0 && W<200) //The range of Weight of average adult human being
{
return true;
}
return false;
}
float CalBMI(int H, int W)
{
return (H/W)*703;
}
int main()
{
float Height, Weight;
cout<<"Enter Height(in inches) and Weight(in lbs) of the person : "<<endl;
cin>>Height;
cin>>Weight;
if(checkHeight(Height) && checkWeight(Weight))cout<<"The BMI is : "<<CalBMI(Height, Weight);
else cout<<"Inputs are not valid"<<endl;
return 0;
}
Vulnerability: Failure to Validate Input Check each line of code Type each Variable that is input Height and Weight Type the Variable which the following check is applicable to the variable: 1. Check length? NA 2. Check range(reasonableness)? Height, Weight 3. Check format? NA 4. Check type? Height, Weight (Automatically done by compiler)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.