Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christian Ballard
- // COSC 1336 - 001
- // Febuary 4, 2015
- // This program should take values from the user and round them using a function.
- #include<iostream>
- #include<limits>
- #include<iomanip>
- #include<cctype>
- #include<string>
- using namespace std;
- // Prototypes
- double round(double, int);
- int readInt(string errorMsg = "", const int = INT_MIN, const int = INT_MAX);
- double readDouble(string errorMsg, double min, double max);
- int main()
- {
- double value; // Stores the number the user wants to round.
- int decPlaces; // Stores the number of decimal place the user wants to round to.
- double decPlaces2; // Dublicates the users input to keep from altering it when calling a function.
- cout << "Please enter the number you would like to be rounded. \n You can enter 0 to exit. " << endl;
- value = readDouble("The number", -DBL_MAX, DBL_MAX);
- cout << "What would you like to round to?" << endl;
- decPlaces = readInt("The number", -6, 6);
- decPlaces2 = round(value, decPlaces);
- cout << fixed << setprecision(6);
- cout << "Your rounded number is " << decPlaces2 << endl;
- system("pause");
- return 0;
- }
- // Takes the users input and rounds it to the decimal place the user inputs.
- double round(double userWhole, int userDecimal)
- {
- bool negative = false;
- double qwerty;
- if (userWhole < 0) {
- negative = true;
- userWhole = fabs(userWhole);
- }
- qwerty = pow(10.0, userDecimal);
- userWhole *= qwerty;
- userWhole += .5;
- userWhole = floor(userWhole);
- userWhole /= qwerty;
- if (negative == true) {
- userWhole *= -1;
- }
- return userWhole;
- }
- //*****************************readInt************************************
- //Description: Reads and validates an integer in the range between min and max.
- // errorMsg: describes to user the type of data to reenter if error encountered
- // min: the minimum acceptable value
- // max: the maximum acceptable value
- //Precondition: user has been prompted for input
- // errorMsg may be assigned a value, default argument is empty string
- // min and max may be assigned values, default arguments are system defaults for int
- // uses header files: iostream, string, limits, cctype
- // postcondition: a valid integer in the range min to max is returned
- int readInt(string errorMsg, int min, int max)
- {
- int someInt; // Stores each integer entered
- bool valid = false; // Loop control flag indicating validdity of entry
- // initially assuming not valid.
- do
- {
- // Read an ineger delimited by a whitespace character
- cin >> someInt;
- // Valid if within min to max range.
- valid = someInt >= min && someInt <= max;
- // Detect and remove invalid input
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << " must be a whole "
- << "number between " << min << " and " << max << ". \nTry Again: ";
- cin.clear(); // Clears the fail state.
- // Remove up to max number of chars in input stream or up to '\n'.
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; // No valid entry has been read.
- }
- } while (!valid); // Loop again, prompting user to re-enter a valid integer.
- // When valid input received, remove from input stream up to 100 remaining
- // characters or until reaches the '\n' <ENTER> character.
- cin.ignore(100, '\n');
- return someInt; // Returns a valid integer to calling environment.
- }
- double readDouble(string errorMsg, const double min, const double max)
- {
- double someDouble; // Stores each double entered.
- bool valid = false; // Loop control flag indeicating validdity of entry
- // initially assuming not valid.
- do
- {
- // Read an integer delimited by a whitespace character.
- cin >> someDouble;
- // Valid if within min to max range.
- valid = someDouble >= min && someDouble <= max;
- // Detect and remove invalid input.
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << errorMsg << " must be a whole "
- << "number between " << min << " and " << max << ". \nTry Again: ";
- cin.clear(); // Clears the fail state.
- // Remove up to max number of chars in input stream or up to '\n'.
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; // No valid entry has been read.
- }
- } while (!valid); // Loop again, prompting user to re-enter a valid integer.
- // When valid input received, remove from input stream up to 100 remaining
- // characters or until reaches the '\n' <ENTER> character.
- cin.ignore(100, '\n');
- return someDouble; // Returns a valid double to calling environment.
- }
Advertisement
Add Comment
Please, Sign In to add comment