// Torrez, Elaine CS1A Chapter 8 P. 487, #1
/********************************************************************************************
*
* VALIDATE CHARGE ACCOUNT NUMBER
*
* ------------------------------------------------------------------------------------------
* This program determines whether a user-entered charge account number is valid.
* The program stores a fixed list of valid account numbers inside an array and then
* performs a linear search to check if the user’s number exists in the list.
* If the number is found, the program reports that the account number is valid;
* otherwise, it reports that it is invalid.
*
* Input validation ensures that the user enters a positive 7-digit number before
* performing the search.
* ------------------------------------------------------------------------------------------
*
* INPUT
* userNum : Charge account number entered by the user
*
* OUTPUT
* Message stating whether the account number is valid or invalid
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 18; // Number of account numbers
int accounts[SIZE] = { // List of valid account numbers
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002
};
int userNum; // Number entered by the user
bool found = false; // Indicates if number is in array
// -------------------------------
// INPUT VALIDATION LOOP
// -------------------------------
cout << "Enter a 7-digit charge account number: ";
cin >> userNum;
while (cin.fail() || userNum < 1000000 || userNum > 9999999)
{
cin.clear(); // Clear input error flag
cin.ignore(1000, '\n'); // Ignore invalid input
cout << "ERROR: Enter a VALID 7-digit positive number: ";
cin >> userNum;
}
// -------------------------------
// LINEAR SEARCH
// -------------------------------
for (int i = 0; i < SIZE; i++)
{
if (accounts[i] == userNum)
{
found = true;
break;
}
}
cout << endl;
cout << fixed << setprecision(0);
// -------------------------------
// OUTPUT RESULTS
// -------------------------------
if (found)
cout << "The account number is VALID." << endl;
else
cout << "The account number is INVALID." << endl;
return 0;
}