Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Christian Ballard
- //COSC 1336 - 001
- //January 28, 2015
- //This program should take the users input of three digits and display them separately
- #include<iostream>
- #include<string>
- #include<limits>
- #include<cctype>
- using namespace std;
- //Prototypes
- int readInt(string errorMsg = "", int min = INT_MIN, int max = INT_MAX);
- int main()
- {
- int number; // Will store the three digit number the user enters.
- int n1, n2, n3; // Will store the extracted digits.
- int number2; // Will store the original user input because the original becomes altered.
- const int ZERO = 0; // Named constant for 0.
- cout << "Please enter a value between 1 and 999. Your may enter '0' to exit." << endl;
- number = readInt("The Number", 0, 999);
- number2 = number; // Duplicates the users input.
- while (number2 != ZERO) // Checks to see if the user entered a '0'.
- {
- //Extraction process
- n1 = number % 10;
- number /= 10;
- n2 = number % 10;
- number /= 10;
- n3 = number % 10;
- number /= 10;
- cout << "The digits in the number " << number2 << " are " << n3 << ", " << n2 << ", and " << n1 << "." << endl;
- cout << "Please enter a value between 1 and 999. Your may enter '0' to exit." << endl;
- number = readInt("The Number", 0, 999);
- number2 = number;
- }
- cout << "Thanks for using this program!" << endl;
- system("pause");
- return 0;
- }
- /*
- *************************************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, sting, 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 validity of entry
- // initially assuming not valid.
- do
- {
- // Read an integer 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" << 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 someInt; // Returns a valid integer to calling environment.
- }
Advertisement
Add Comment
Please, Sign In to add comment