steamengines

PA 4 - Input Validation

Feb 2nd, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. //Christian Ballard
  2. //COSC 1336 - 001
  3. //January 28, 2015
  4. //This program should take the users input of three digits and display them separately
  5. #include<iostream>
  6. #include<string>
  7. #include<limits>
  8. #include<cctype>
  9. using namespace std;
  10.  
  11. //Prototypes
  12. int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
  13.  
  14. int main()
  15. {
  16.  
  17.     int number; // Will store the three digit number the user enters.
  18.     int n1, n2, n3; // Will store the extracted digits.
  19.     int number2; // Will store the original user input because the original becomes altered.
  20.     const int ZERO = 0; // Named constant for 0.
  21.  
  22.     cout << "Please enter a value between 1 and 999. Your may enter '0' to exit." << endl;
  23.  
  24.     number = readInt("The Number", 0, 999);
  25.  
  26.     number2 = number; // Duplicates the users input.
  27.  
  28.     while (number2 != ZERO) // Checks to see if the user entered a '0'.
  29.     {
  30.            
  31.         //Extraction process
  32.         n1 = number % 10;
  33.         number /= 10;
  34.         n2 = number % 10;
  35.         number /= 10;
  36.         n3 = number % 10;
  37.         number /= 10;
  38.  
  39.  
  40.         cout << "The digits in the number " << number2 << " are " << n3 << ", " << n2 << ", and " << n1 << "." << endl;
  41.  
  42.         cout << "Please enter a value between 1 and 999. Your may enter '0' to exit." << endl;
  43.  
  44.         number = readInt("The Number", 0, 999);
  45.  
  46.         number2 = number;
  47.     }
  48.     cout << "Thanks for using this program!" << endl;
  49.  
  50.     system("pause");
  51.     return 0;
  52. }
  53. /*
  54. *************************************readInt***********************************
  55. description: Reads and validates an integer in the range between min and max.
  56.         errorMsg: Describes to user the type of data to reenter if error encountered
  57.         min:    The minimum acceptable value.
  58.         max:    The maximum acceptable value.
  59. precondition: User has been prompted for input
  60.     errorMsg may be assigned a value, default argument is empty string
  61.     min and max may be assigned values, default arguments are system defaults for int
  62.     uses header files:  iostream, sting, limits, cctype.
  63. postcondition: A valid integer in the range min to max is returned.
  64. */
  65. int readInt(string errorMsg, int min, int max)
  66. {
  67.     int someInt;            // Stores each integer entered
  68.     bool valid = false; // Loop control flag indicating validity of entry
  69.     // initially assuming not valid.
  70.     do
  71.     {
  72.         // Read an integer delimited by a whitespace character
  73.         cin >> someInt;
  74.  
  75.         // Valid if within min to max range.
  76.         valid = someInt >= min && someInt <= max;
  77.  
  78.         // Detect and remove invalid input
  79.         if (cin.fail() || !valid || !isspace(cin.peek()))
  80.         {
  81.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
  82.                 << "number between " << min << " and " << max << ". \nTry Again: ";
  83.  
  84.             cin.clear();            // Clears the fail state.
  85.  
  86.             // Remove up to max number of chars in input stream or up to '\n'.
  87.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  88.  
  89.             valid = false;          // No valid entry has been read.
  90.         }
  91.  
  92.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  93.  
  94.     // When valid input received, remove from input stream up to 100 remaining
  95.     // characters or until reaches the '\n' <ENTER> character.
  96.     cin.ignore(100, '\n');
  97.  
  98.     return someInt; // Returns a valid integer to calling environment.
  99. }
Advertisement
Add Comment
Please, Sign In to add comment