steamengines

PA 5 - Round Function

Feb 12th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1336 - 001
  3. // Febuary 4, 2015
  4. // This program should take values from the user and round them using a function.
  5. #include<iostream>
  6. #include<limits>
  7. #include<iomanip>
  8. #include<cctype>
  9. #include<string>
  10. using namespace std;
  11.  
  12. // Prototypes
  13. double round(double, int);
  14. int readInt(string errorMsg = "", const int = INT_MIN, const int = INT_MAX);
  15. double readDouble(string errorMsg, double min, double max);
  16.  
  17. int main()
  18. {
  19.     double value;   // Stores the number the user wants to round.
  20.     int decPlaces;  // Stores the number of decimal place the user wants to round to.
  21.     double decPlaces2; // Dublicates the users input to keep from altering it when calling a function.
  22.  
  23.  
  24.     cout << "Please enter the number you would like to be rounded. \n You can enter 0 to exit. " << endl;
  25.     value = readDouble("The number", -DBL_MAX, DBL_MAX);
  26.  
  27.  
  28.     cout << "What would you like to round to?" << endl;
  29.     decPlaces = readInt("The number", -6, 6);
  30.  
  31.  
  32.     decPlaces2 = round(value, decPlaces);
  33.  
  34.  
  35.     cout << fixed << setprecision(6);
  36.  
  37.     cout << "Your rounded number is " << decPlaces2 << endl;
  38.  
  39.  
  40.  
  41.     system("pause");
  42.     return 0;
  43. }
  44. // Takes the users input and rounds it to the decimal place the user inputs.
  45. double round(double userWhole, int userDecimal)
  46. {
  47.     bool negative = false;
  48.     double qwerty;
  49.  
  50.     if (userWhole < 0) {
  51.         negative = true;
  52.         userWhole = fabs(userWhole);
  53.     }
  54.  
  55.     qwerty = pow(10.0, userDecimal);
  56.     userWhole *= qwerty;
  57.     userWhole += .5;
  58.     userWhole = floor(userWhole);
  59.     userWhole /= qwerty;
  60.  
  61.     if (negative == true) {
  62.         userWhole *= -1;
  63.     }
  64.  
  65.     return userWhole;
  66. }
  67.  
  68. //*****************************readInt************************************
  69. //Description: Reads and validates an integer in the range between min and max.
  70. //      errorMsg: describes to user the type of data to reenter if error encountered
  71. //      min: the minimum acceptable value
  72. //      max: the maximum acceptable value
  73. //Precondition: user has been prompted for input
  74. //  errorMsg may be assigned a value, default argument is empty string
  75. //  min and max may be assigned values, default arguments are system defaults for int
  76. //  uses header files: iostream, string, limits, cctype
  77. // postcondition: a valid integer in the range min to max is returned
  78. int readInt(string errorMsg, int min, int max)
  79. {
  80.     int someInt;        // Stores each integer entered
  81.     bool valid = false; // Loop control flag indicating validdity of entry
  82.                         // initially assuming not valid.
  83.     do
  84.     {
  85.         // Read an ineger delimited by a whitespace character
  86.         cin >> someInt;
  87.  
  88.         // Valid if within min to max range.
  89.         valid = someInt >= min && someInt <= max;
  90.  
  91.         // Detect and remove invalid input
  92.         if (cin.fail() || !valid || !isspace(cin.peek()))
  93.         {
  94.             cout << "\nError! Invalid input.\n" << " must be a whole "
  95.                 << "number between " << min << " and " << max << ". \nTry Again: ";
  96.  
  97.             cin.clear();        // Clears the fail state.
  98.  
  99.             // Remove up to max number of chars in input stream or up to '\n'.
  100.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  101.  
  102.             valid = false;      // No valid entry has been read.
  103.         }
  104.  
  105.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  106.  
  107.     // When valid input received, remove from input stream up to 100 remaining
  108.     // characters or until reaches the '\n' <ENTER> character.
  109.     cin.ignore(100, '\n');
  110.  
  111.     return someInt; // Returns a valid integer to calling environment.
  112. }
  113.  
  114. double readDouble(string errorMsg, const double min, const double max)
  115. {
  116.     double someDouble;      // Stores each double entered.
  117.     bool valid = false;     // Loop control flag indeicating validdity of entry
  118.     // initially assuming not valid.
  119.     do
  120.     {
  121.         // Read an integer delimited by a whitespace character.
  122.         cin >> someDouble;
  123.  
  124.         // Valid if within min to max range.
  125.         valid = someDouble >= min && someDouble <= max;
  126.  
  127.         // Detect and remove invalid input.
  128.         if (cin.fail() || !valid || !isspace(cin.peek()))
  129.         {
  130.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
  131.                 << "number between " << min << " and " << max << ". \nTry Again: ";
  132.  
  133.             cin.clear();        // Clears the fail state.
  134.  
  135.             // Remove up to max number of chars in input stream or up to '\n'.
  136.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  137.  
  138.             valid = false;      // No valid entry has been read.
  139.         }
  140.     } while (!valid); // Loop again, prompting user to re-enter a valid integer.
  141.  
  142.     // When valid input received, remove from input stream up to 100 remaining
  143.     // characters or until reaches the '\n' <ENTER> character.
  144.     cin.ignore(100, '\n');
  145.  
  146.     return someDouble; // Returns a valid double to calling environment.
  147. }
Add Comment
Please, Sign In to add comment