Bukz

n00b input validation...

Apr 11th, 2011
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // What is likely a far too complex/cludgy/etc. attempt at basic input validation? :s
  2. #include <cctype>  // for isalpha() - isdigit() - and friends
  3. #include <cstdlib> // for atoi()
  4. #include <cstring> // for strlen()
  5. #include <iomanip> // for setw()
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. // Input validation function prototypes
  11. bool isWholeNum (char[]);
  12. bool isAlphaStr (char[]);
  13.  
  14. int main()
  15. {
  16.     char a;
  17.     char firstName[16], lastName[16], age[4];
  18.  
  19.     do
  20.     {
  21.         cout << "\nEnter your first name: ";
  22.         cin >> setw(16) >> firstName;
  23.         cin.ignore(1000, '\n');
  24.  
  25.         // Catch errors and loop the get input stage until proper input has been entered
  26.         while (!isAlphaStr(firstName) || !(strlen(firstName) > 1))
  27.         {
  28.             cout << "\nError: invalid input, please try again.\n";
  29.             cout << " Hint: only use alphabetical characters.\n";
  30.  
  31.             cout << "\nEnter your first name: ";
  32.             cin >> setw(16) >> firstName;
  33.             cin.ignore(1000, '\n');
  34.         }
  35.        
  36.         // Once we've gotten a proper response to the first question, continue
  37.         cout << "Enter your last name: ";
  38.         cin >> setw(16) >> lastName;
  39.         cin.ignore(1000, '\n');
  40.  
  41.         // Catch errors and loop the get input stage until proper input has been entered
  42.         while (!isAlphaStr(lastName) || !(strlen(lastName) > 1))
  43.         {
  44.             cout << "\nError: invalid input, please try again.\n";
  45.             cout << " Hint: only use alphabetical characters.\n";
  46.  
  47.             cout << "\nEnter your last name: ";
  48.             cin >> setw(16) >> lastName;
  49.             cin.ignore(1000, '\n');
  50.         }
  51.  
  52.         // Once we've gotten a proper response to the second question, continue
  53.         cout << "Enter your age: ";
  54.         cin >> setw(4) >> age;
  55.         cin.ignore(1000, '\n');
  56.  
  57.         // Catch errors and loop the get input stage until proper input has been entered
  58.         while (!isWholeNum(age) || !(atoi(age) > 0))
  59.         {
  60.             cout << "\nError: invalid input, please try again.\n";
  61.             cout << " Hint: only use positive, whole numbers.\n";
  62.  
  63.             cout << "\nEnter your age: ";
  64.             cin >> setw(4) >> age;
  65.             cin.ignore(1000, '\n');
  66.         }
  67.  
  68.         // Once we've gotten a proper response to the last question, continue
  69.         cout << "\nGreetings, " << firstName << " " << lastName << " (" << age << ")\n";
  70.  
  71.         cout << "\nRun the program again? y/n? ";
  72.         cin.get(a);
  73.         cin.ignore();
  74.     }
  75.     while (tolower(a) == 'y');
  76.     return 0;
  77. }
  78.  
  79. // Determines if user input is a positive whole number
  80. bool isWholeNum (char cArray[])
  81. {
  82.     unsigned short index = 0;
  83.    
  84.     while(cArray[index] != '\0')
  85.     {
  86.         if (!isdigit(cArray[index]))
  87.             return false;
  88.         ++index;
  89.     }
  90.  
  91.     return true;
  92. }
  93.  
  94. // Determines if user input is a string of nothing but alphabetical characters
  95. bool isAlphaStr (char cArray[])
  96. {
  97.     unsigned short index = 0;
  98.    
  99.     while(cArray[index] != '\0')
  100.     {
  101.         if (!isalpha(cArray[index]))
  102.             return false;
  103.         ++index;
  104.     }
  105.  
  106.     return true;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment