fork download
  1. //Sam Partovi CS1A Chapter 8, P 487, #1
  2. //
  3. /*******************************************************************************
  4. * VALIDATE ACCOUNT NUMBER
  5. * ____________________________________________________________
  6. * This program validates a given account number against a set of specified
  7. * valid account numbers, and determines whether the number is part of the set or
  8. * not.
  9. * ____________________________________________________________
  10. *INPUT
  11. * NUMBER_COUNT: Array size for account numbers
  12. * inputNum: Given number to be validated
  13. * numArray: Array of valid account numbers
  14. *OUTPUT
  15. * validFlag: Flag to check for validity of numbers
  16. ******************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. int main() {
  22. const int NUMBER_COUNT = 18; //INPUT - Array size for account numbers
  23. int inputNum; //INPUT - Given number to be validated
  24.  
  25. //INPUT - Array of valid account numbers
  26. int numArray[NUMBER_COUNT] = {5658845, 4520125, 7895122, 8777541, 8451277,
  27. 1302850, 8080152, 4562555, 5552012, 5050552,
  28. 7825877, 1250255, 1005231, 6545231, 3852085,
  29. 7576651, 7881200, 4581002};
  30.  
  31. bool validFlag; //OUTPUT - Flag to check for validity of numbers
  32.  
  33. //Prompt for account number input
  34. cout << "Enter a charge account number:";
  35. cin >> inputNum;
  36.  
  37. //Validate if inputNum is found in the array
  38. for(int i = 0; i < NUMBER_COUNT; i++) {
  39. if(numArray[i] == inputNum) {
  40. validFlag = true;
  41. }
  42. }
  43.  
  44. //Display results based on flag value
  45. if(validFlag == true) {
  46. cout << "\n" << inputNum << " is a valid number.\n";
  47. }
  48. else cout << "\n" << inputNum << " is an invalid number.\n";
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5280KB
stdin
5658845
stdout
Enter a charge account number:
5658845 is a valid number.