steamengines

PA 6 - readDouble

Feb 12th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 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 = "", int min = INT_MIN, int 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. double readDouble(string errorMsg, int min, int max)
  44. {
  45.     double someDouble;      // Stores each double entered.
  46.     bool valid = false;     // Loop control flag indeicating validdity of entry
  47.                             // initially assuming not valid.
  48.     do
  49.     {
  50.         // Read an integer delimited by a whitespace character.
  51.         cin >> someDouble;
  52.  
  53.         // Valid if within min to max range.
  54.         valid = someDouble >= min && someDouble <= max;
  55.  
  56.         // Detect and remove invalid input.
  57.         if (cin.fail() || !valid || !isspace(cin.peek()))
  58.         {
  59.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
  60.                 << "number between " << min << " and " << max << ". \nTry Again: ";
  61.  
  62.             cin.clear();        // Clears the fail state.
  63.  
  64.             // Remove up to max number of chars in input stream or up to '\n'.
  65.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  66.  
  67.             valid = false;      // No valid entry has been read.
  68.         }
  69.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  70.  
  71.     // When valid input received, remove from input stream up to 100 remaining
  72.     // characters or until reaches the '\n' <ENTER> character.
  73.     cin.ignore(100, '\n');
  74.  
  75.     return someDouble; // Returns a valid double to calling environment.
  76. }
Advertisement
Add Comment
Please, Sign In to add comment