By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Validating Whole Numbers

P: 16
Simple bubbling sort algorithm, nothing fancy, just trying to find a way to validate each number value as it's entered without creating several arrays and loops using isdigit(). I need to validate that the "pass" int is a whole number value and that each "number" int are whole number values. Any help would be appreciated, I'm stuck out of ideas.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. const int MaxNum = 21;
  7. int Array[MaxNum]; 
  8. int i, j, k, l, number, pass, temp;
  9.  
  10. bool IsSorted(false);
  11. void Sort(int Array[], int pass);
  12.  
  13. void main ()
  14.     {
  15.     cout << "-------------------------------------------------------------------------------" << endl;
  16.     cout << "This program will allow you to enter numbers and sort them in increasing value." << endl;
  17.     cout << "-------------------------------------------------------------------------------" << endl;
  18.     cout << "First, how many numbers you will enter? (Must be less than 20): ";
  19.     cin >> pass;
  20.  
  21. //validate pass
  22.  
  23.     while (pass < 1 || pass > 20)
  24.         { 
  25.         cout << "ERROR: You cannot enter less than 1 or more than 20 numbers. Try again: ";
  26.         cin >> pass;
  27.         }
  28.  
  29.     for (i=0; i<pass; i++)
  30.         {
  31.         cout << "Enter a whole number: ";
  32.         cin >> number;
  33.  
  34.         Array[i] = number;
  35.         }
  36.  
  37.     cout << "Unsorted: ";
  38.         for (k=0; k<pass; k++)
  39.             cout << Array[k]<< " ";
  40.  
  41.     Sort(Array, pass);
  42.  
  43.     cout << endl << "Sorted: ";
  44.         for (k=0; k<pass; k++)
  45.             cout << Array[k] << " ";
  46.         cout << endl;
  47.     }
  48.  
  49. void Sort(int Array[], int pass)
  50.     {
  51.     while(!IsSorted)
  52.         {
  53.         IsSorted=true;
  54.         for (j=0; j<pass-1; j++)
  55.             {
  56.             if (Array[j]>Array[j+1])
  57.                 {
  58.                 temp=Array[j];
  59.                 Array[j]=Array[j+1];
  60.                 Array[j+1]=temp;
  61.                 IsSorted=false;
  62.                 } 
  63.             }
  64.         } 
  65.     }
  66.  
Mar 20 '07 #1
Share this Question
Share on Google+
5 Replies


Roonie
P: 99
im pretty sure that an int will hold only whole numbers . . . therefore, you need not worry about checking them.

i wrote and ran this:
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     int i = 0;
  9.     cin >> i;
  10.     cout << i;
  11.     system("PAUSE");
  12.     return EXIT_SUCCESS;
  13. }
and i could give it any decimal number and it would give back out only the integer (whole) part of the number.
Mar 20 '07 #2

P: 16
I was thinking more along the lines of ruling out all characters and punctuation so that the only acceptable user input is numerical values. But isdigit() would require me to turn input into an array and then run a loop to check each cell.

Is there any way to bypass that?
Mar 20 '07 #3

Roonie
P: 99
im still using my little test program, and i believe that trying to store a non-number in an int results in no change to the int.

so if you set your input variable to zero, say, or some other unacceptable number and then taking an input value, you can just check that (input != 0) to make sure that you really did get a number.
Mar 20 '07 #4

Roonie
P: 99
try this:

Expand|Select|Wrap|Line Numbers
  1. int input = 0;
  2.  
  3. do
  4. {
  5. cout << "please enter a number between 1 and 20:"
  6. cin >> input;
  7. } while(input == 0 && <other checks here>);
  8.  
Mar 20 '07 #5

Expert 100+
P: 1,510
I was thinking more along the lines of ruling out all characters and punctuation so that the only acceptable user input is numerical values. But isdigit() would require me to turn input into an array and then run a loop to check each cell.

Is there any way to bypass that?
you can try somethimg along the lines of
Expand|Select|Wrap|Line Numbers
  1.     int n;
  2.     while (cin >> n, !cin.eof())                     // read number, if EOF terminate    
  3.         if (cin.fail() || ! isspace(cin.peek()))               // error during read ? 
  4.             {
  5.             cin.clear();                                           // clear any error 
  6.             cout << "\n  error at '" << char(cin.get()) << "' try again  ? ";
  7.             cin.ignore(1000, '\n');                  // discard characters to newline
  8.             }            
  9.         else
  10.             cout << n << " OK  ";                                   // number read OK
  11.  
will keep reading ints rejecting input until a valid integer is entered - remember if an error occurs you have to call clear() to clear the error condition
Mar 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.