446,238 Members | 1,771 Online
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 #include  #include    using namespace std;   const int MaxNum = 21; int Array[MaxNum];  int i, j, k, l, number, pass, temp;   bool IsSorted(false); void Sort(int Array[], int pass);   void main ()     {     cout << "-------------------------------------------------------------------------------" << endl;     cout << "This program will allow you to enter numbers and sort them in increasing value." << endl;     cout << "-------------------------------------------------------------------------------" << endl;     cout << "First, how many numbers you will enter? (Must be less than 20): ";     cin >> pass;   //validate pass       while (pass < 1 || pass > 20)         {          cout << "ERROR: You cannot enter less than 1 or more than 20 numbers. Try again: ";         cin >> pass;         }       for (i=0; i> number;           Array[i] = number;         }       cout << "Unsorted: ";         for (k=0; kArray[j+1])                 {                 temp=Array[j];                 Array[j]=Array[j+1];                 Array[j+1]=temp;                 IsSorted=false;                 }              }         }      }   Mar 20 '07 #1
5 Replies

 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 #include  #include    using namespace std;   int main(int argc, char *argv[]) {     int i = 0;     cin >> i;     cout << i;     system("PAUSE");     return EXIT_SUCCESS; } 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

 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

 P: 99 try this: Expand|Select|Wrap|Line Numbers int input = 0;   do { cout << "please enter a number between 1 and 20:" cin >> input; } while(input == 0 && );   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     int n;     while (cin >> n, !cin.eof())                     // read number, if EOF terminate             if (cin.fail() || ! isspace(cin.peek()))               // error during read ?              {             cin.clear();                                           // clear any error              cout << "\n  error at '" << char(cin.get()) << "' try again  ? ";             cin.ignore(1000, '\n');                  // discard characters to newline             }                     else             cout << n << " OK  ";                                   // number read OK   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