Answers – Input Validation – CS2 – C++



Program 1


2. List the potential input validation errors.


The index used returned by getWhich is not validated. If a non-integer value is typed for the prompt getWhich(), an exception will be thrown.


3. Provide example inputs that might cause validation problems, and describe the problems that they might cause.


If the number typed for getWhich() is greater SIZE or it is less than zero the value passed to getName() will be out of bounds, and the results might be unpredictable.

See next question as well.


4. What happens if you type non-numeric characters for either the number of names or which name you wanted to retrieve?


The result will be unpredictable – the program might crash, or you might get no response at all.


5.Revise the program to properly validate input and gracefully recover from errors.

#include 
using namespace std;

void getNames(string[], int);
int getWhich();
string getName(int, string[],int);
bool isNum(string);
bool isNum(char);
int getInteger(string);

const int SIZE=10;

int main() {

  
  int which;

  string names[SIZE];
  getNames(names,SIZE);
  
  which =getWhich();

  string aName = getName(which,names,SIZE);
  cout > names[i];
  }
}
  

int getWhich() {

  int x;
  bool good = false;
  
  while (good == false) {
    cout > indexString;
    // first, verify that the string is made up of digits.
    if (isNum(indexString)  == true) {
      // if it is, convert it to an integer
      x = getInteger(indexString);
      // if the result is in the right range, we’re set
      if (x >0 && x ='0' && c =1 && n 


The original program did not validate the name for format or length, and neither does this version. Validating names is extremely difficult, as puncuation characters, digits, and other non-alphabetic characters might appear.

Program 2


#include 
using namespace std; 

string getPriceString();
string getQuantString();
string getStringValue(string);
bool validatePrice(string);
bool validateQuant(string);
bool isNum(char);
void printConfirmation(int,float);
int getIntVal(string);
float getFloatVal(string);

int main() {

  bool priceValid = false;
  bool quantValid = false;

  string priceString="";
  string quantString="";
   
  float price=0.0f;
  int quant = 0; 
    
  while (priceValid == false || quantValid == false) {
    if (priceValid == false) {
      priceString =  getPriceString();
      priceValid =  validatePrice(priceString);
      if (priceValid == true) {
       price = getFloatVal(priceString);
      }
    }
      
      if (quantValid == false) {
        quantString = getQuantString();
        quantValid = validateQuant(quantString);
        if (quantValid == true) {
          quant = getIntVal(quantString);
        }
      }
      
      if (priceValid == false) {
        cout > value;
  return value;
}
  
bool validatePrice(string price) {
    int decimalCount = 0;
    bool seenDigit = false;
    
    for (int i=0; i ='0' && c 0
    int quan = getIntVal(q);
    if (quan 1) {
      item +="s";
    }
    cout  1) {
      cout (num);
  for (int i =0; i 


1. You’re writing a program that asks the user to type in a telephone number. How might you validate that the characters that they’ve typed represent a legal telephone number? You should assume that you’re only concerned about phone numbers from the US, but you want to give users as much flexibility as possible, in terms of spaces and punctuation characters. List some rules that you might use. Make sure that you complete this question before moving on to question #2.

  1. Verify that there are 10 digits
  2. Remove any parentheses, dashes, or spaces


2. Find an example of a phone number that doesn’t fit your rules.


Anything that requires a leading 1 – as in 1 410 555 1212

Any number specified with a “+” at the beginning: +1 410 555 1212


3. Describe either an example of an input validation problem that you may have encountered. If you can’t remember having any sort of problem, try some web pages or other software tools – try to find a system that fails to validate input data correctly.


Taking zip codes without verifying 5 digits, accepting dates that have already passed, improper formats for phone numbers, etc.


Further Work


1. If input is sufficiently cryptic, it might be hard to provide useful error messages in responses to invalid input. Describe some strategies that might be used to help users recover from invalid input.


Example formats indicating what correct inputs might look like, error messages that describe difficulties with input as provided. Flexible inputs that allow users to correct multiple errors with one screen – as opposed to fixing them one at a time.


2. Revisit Program 2. Are there any inputs that the above description accepts as valid that perhaps should be considered invalid? If so, what are they and how might you handle them?


The price can accept value with three numbers after the decimal point, such as 12.345. This should probably be modified so that the number of cents is required to be whole.


View more information: http://cis1.towson.edu/~cssecinj/modules/cs2/input-validation-cs2-c/answers-input-validation-cs2-c/

See more articles in category: Grammar
READ:  iPhone 6 vs Galaxy S6

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button