steamengines

PA 3 Extracted digits

Jan 26th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. //Christian Ballard
  2. //COSC 1336 - 001
  3. //January 28, 2015
  4. //This program should take the users input of three digits and display them seperatly
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.    
  11.     int number; // Will store the three digit number the user enters.
  12.     int n1, n2, n3; // Will store the extracted digits.
  13.     int number2; // Will store the original user input because the original becomes altered.
  14.    
  15.     do
  16.     {
  17.  
  18.         cout << "Please enter a value between 1 and 999. Your may enter '0' to exit." << endl;
  19.         cin >> number;
  20.         number2 = number; // Duplicates the users input.
  21.  
  22.         if (number == 0) // Checks to see if the user entered a '0'.
  23.         {
  24.             cout << "Thanks for using this program!" << endl;
  25.         }
  26.  
  27.         else if (number < 0 || number > 999) // Checks to see if the inputed number is in range.
  28.         {
  29.             cout << "That number is invalid please try again." << endl;
  30.         }
  31.         else // If everything above checks out the extraction will proceed.
  32.         {
  33.             //Extraction process
  34.             n1 = number % 10;
  35.             number /= 10;
  36.             n2 = number % 10;
  37.             number /= 10;
  38.             n3 = number % 10;
  39.             number /= 10;
  40.  
  41.  
  42.             cout << "The digits in the number " << number2 << " are " << n3 << ", " << n2 << ", and " << n1 << "." << endl;
  43.         }
  44.     } while (number2 != 0); // Checks to see if the program needs to keep running.
  45.  
  46.  
  47.     system("pause");
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment