fork download
  1. // Torrez, Elaine CS1A Chapter 8 P. 487, #1
  2. /********************************************************************************************
  3.  *
  4.  * VALIDATE CHARGE ACCOUNT NUMBER
  5.  *
  6.  * ------------------------------------------------------------------------------------------
  7.  * This program determines whether a user-entered charge account number is valid.
  8.  * The program stores a fixed list of valid account numbers inside an array and then
  9.  * performs a linear search to check if the user’s number exists in the list.
  10.  * If the number is found, the program reports that the account number is valid;
  11.  * otherwise, it reports that it is invalid.
  12.  *
  13.  * Input validation ensures that the user enters a positive 7-digit number before
  14.  * performing the search.
  15.  * ------------------------------------------------------------------------------------------
  16.  *
  17.  * INPUT
  18.  * userNum : Charge account number entered by the user
  19.  *
  20.  * OUTPUT
  21.  * Message stating whether the account number is valid or invalid
  22.  *
  23.  ********************************************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27. using namespace std;
  28.  
  29. int main()
  30. {
  31. const int SIZE = 18; // Number of account numbers
  32. int accounts[SIZE] = { // List of valid account numbers
  33. 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  34. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  35. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002
  36. };
  37.  
  38. int userNum; // Number entered by the user
  39. bool found = false; // Indicates if number is in array
  40.  
  41. // -------------------------------
  42. // INPUT VALIDATION LOOP
  43. // -------------------------------
  44. cout << "Enter a 7-digit charge account number: ";
  45. cin >> userNum;
  46.  
  47. while (cin.fail() || userNum < 1000000 || userNum > 9999999)
  48. {
  49. cin.clear(); // Clear input error flag
  50. cin.ignore(1000, '\n'); // Ignore invalid input
  51.  
  52. cout << "ERROR: Enter a VALID 7-digit positive number: ";
  53. cin >> userNum;
  54. }
  55.  
  56. // -------------------------------
  57. // LINEAR SEARCH
  58. // -------------------------------
  59. for (int i = 0; i < SIZE; i++)
  60. {
  61. if (accounts[i] == userNum)
  62. {
  63. found = true;
  64. break;
  65. }
  66. }
  67.  
  68. cout << endl;
  69. cout << fixed << setprecision(0);
  70.  
  71. // -------------------------------
  72. // OUTPUT RESULTS
  73. // -------------------------------
  74. if (found)
  75. cout << "The account number is VALID." << endl;
  76. else
  77. cout << "The account number is INVALID." << endl;
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0s 5320KB
stdin
5658845

4520125

7895122

8777541

8451277

1302850

8080152

4562555

5552012

5050552

7825877

1250255

1005231

6545231

3852085

7576651

7881200

4581002
stdout
Enter a 7-digit charge account number: 
The account number is VALID.