fork download
  1. //Ryan Shahriyarpour CS1A Chapter 8 Question 1
  2. /*****************************************************************************
  3.  *
  4.  * Charge Account Validation
  5.  *
  6.  *********************
  7.  *
  8.  * This program lets the user enter a charge account number and determines
  9.  * whether the number is valid by checking for it in a list of 18 valid
  10.  * account numbers stored in an array.
  11.  *
  12.  * *******************
  13.  * A LINEAR SEARCH is used. Each element of the array is compared to the
  14.  * number entered, starting at the first element, until a match is found or
  15.  * the end of the array is reached.
  16.  *
  17.  * INPUT
  18.  * account - the charge account number to check
  19.  *
  20.  * OUTPUT
  21.  * A message saying whether the account number is valid or invalid
  22.  *
  23.  ****************************************************************************/
  24.  
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. // Constant for the number of valid accounts
  29. const int SIZE = 18;
  30.  
  31. int main()
  32. {
  33. // data dictionary
  34. int accounts[SIZE] = { // List of valid account numbers
  35. 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  36. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  37. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002
  38. };
  39. int account; // Account number entered by the user
  40. int index; // Loop counter for the search
  41. bool isValid = false; // True if the account number is in the list
  42.  
  43. // Get the account number
  44. cout << "Enter a charge account number: ";
  45. cin >> account;
  46.  
  47. // Linear search
  48. for (index = 0; index < SIZE; index++)
  49. {
  50. if (accounts[index] == account)
  51. {
  52. isValid = true;
  53. break;
  54. }
  55. }
  56.  
  57. // Display the result
  58. if (isValid)
  59. cout << "\nThat account number is valid." << endl;
  60. else
  61. cout << "\nThat account number is invalid." << endl;
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Enter a charge account number: 
That account number is invalid.