fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 8, P. 487, #1
  3.  
  4. /**********************************************************
  5.  * Class: CS1A
  6.  * Chapter 8 Homework - Programming Challenge #1
  7.  * Title: Charge Account Validation
  8.  *
  9.  * Description:
  10.  * This program uses linear search to check whether a charge
  11.  * account number is in the list of valid account numbers.
  12.  *
  13.  * INPUT:
  14.  * A charge account number entered by the user.
  15.  *
  16.  * OUTPUT:
  17.  * A message saying whether the number is valid.
  18.  **********************************************************/
  19.  
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. const int SIZE = 18;
  26. const int accounts[SIZE] = {
  27. 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  28. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  29. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002
  30. };
  31.  
  32. int accountNumber; //INPUT - Charge account number from user
  33. bool found = false;
  34.  
  35. cout << "Enter a charge account number: ";
  36. cin >> accountNumber;
  37.  
  38. // Check each array element until a match is found (linear search).
  39. for (int i = 0; i < SIZE; i++)
  40. {
  41. if (accounts[i] == accountNumber)
  42. {
  43. found = true;
  44. break;
  45. }
  46. }
  47.  
  48. //OUTPUT - Display the result of the search.
  49. if (found)
  50. cout << "The charge account number is valid." << endl;
  51. else
  52. cout << "The charge account number is invalid." << endl;
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5340KB
stdin
5658845
stdout
Enter a charge account number: The charge account number is valid.