steamengines

PA 6 - Formatting readDouble (updated)

Apr 21st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1336 - 001
  3. // This program should validate user input for doubles.
  4. // Febuary 9, 2015
  5. #include<iostream>
  6. #include<string>
  7. #include<limits>
  8. #include<cctype>
  9. using namespace std;
  10.  
  11. //Prototypes
  12. double readDouble(string errorMsg = "", double min = INT_MIN, double max = INT_MAX);
  13.  
  14. int main()
  15. {
  16.     double x;
  17.     do   {
  18.             cout << " Enter a value: ";  
  19.             x = readDouble("test1 use default args");
  20.             cout << " Enter a value: ";
  21.             x = readDouble("test2 1.5,19.5", 1.5, 19.5);
  22.             cout << " Enter a value: ";
  23.             x = readDouble("test3 -2000.5678912,100.567", -2000.5678912, 100.567);
  24.             cout << " Enter a value: ";
  25.             x = readDouble("test4 -9999999,9999999", -9999999, 9999999);
  26.             cout << " Enter a value: ";
  27.             x = readDouble("test5 -929999999.5,929999999.5", -929999999.5, 929999999.5);
  28.             cout << " Enter a value: ";
  29.             x = readDouble("test6 -1,1234567890, default max", -1, 1234567890);
  30.             cout << " Enter a value: ";
  31.             x = readDouble("test7 -1234567890, 100", -1234567890, 100);
  32.             cout << " Enter a value: ";
  33.             x = readDouble("test8 -1234567890,1234567890", -1234567890, 1234567890);
  34.             // etc.  
  35.     } while(true);
  36.  
  37.  
  38.  
  39.     system("pause");
  40.     return 0;
  41. }
  42.  
  43. //*****************************readDouble************************************
  44. //Description: Reads and validates an oulbe in the range between min and max.
  45. //      errorMsg: describes to user the type of data to reenter if error encountered
  46. //      min: the minimum acceptable value
  47. //      max: the maximum acceptable value
  48. //Precondition: user has been prompted for input
  49. //  errorMsg may be assigned a value, default argument is empty string
  50. //  min and max may be assigned values, default arguments are system defaults for int
  51. //  uses header files: iostream, string, limits, cctype
  52. // postcondition: a valid integer in the range min to max is returned
  53. double readDouble(string errorMsg, double min, double max)
  54. {
  55.     double someDouble;      // Stores each double entered.
  56.     bool valid = false;     // Loop control flag indeicating validdity of entry
  57.                             // initially assuming not valid.
  58.     do
  59.     {
  60.         // Read an integer delimited by a whitespace character.
  61.         cin >> someDouble;
  62.  
  63.         // Valid if within min to max range.
  64.         valid = someDouble >= min && someDouble <= max;
  65.  
  66.         // Detect and remove invalid input.
  67.         if (cin.fail() || !valid || !isspace(cin.peek()))
  68.         {
  69.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
  70.                 << "number between " << (min < -10E6 || min > 10E6 || min > -10E-6 && min < 10E-6 ? scientific : fixed) << min
  71.                 << " and " << (max < 10E6 || max > -10E-6 && max < 10E-6 ? scientific : fixed)
  72.                 << max << ". \nTry Again: ";
  73.  
  74.             cin.clear();        // Clears the fail state.
  75.  
  76.             // Remove up to max number of chars in input stream or up to '\n'.
  77.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  78.  
  79.             valid = false;      // No valid entry has been read.
  80.         }
  81.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  82.  
  83.     // When valid input received, remove from input stream up to 100 remaining
  84.     // characters or until reaches the '\n' <ENTER> character.
  85.     cin.ignore(100, '\n');
  86.  
  87.     return someDouble; // Returns a valid double to calling environment.
  88. }
Advertisement
Add Comment
Please, Sign In to add comment