fork download
  1. //Amari Mosley CSC5 Chapter 8, P. 487, #1
  2.  
  3. /*******************************************************************************
  4. *
  5. * COMPUTE CHARGE VALIDATION
  6. * ______________________________________________________________________________
  7. *
  8. * This program lets the user enter a charge account number
  9. * and determines if the number is valid by searching for it
  10. * in an initialized single-dimensional array using a linear search.
  11. *
  12. * INPUT
  13. * accountNumber : The account number entered by the user
  14. *
  15. * OUTPUT
  16. * Validation message : Indicates whether the account number is valid or
  17. * invalid
  18. *
  19. ********************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main ()
  24. {
  25. // Constants
  26. const int NUM_ACCOUNTS = 18;
  27.  
  28. // Array of valid charge account numbers
  29. int accounts[NUM_ACCOUNTS] = { 5658845, 4520125, 7895122, 8777541,
  30. 8451277, 1302850, 8080152, 4562555,
  31. 5552012, 5050552, 7825877, 1250255,
  32. 1005231, 6545231, 3852085, 7576651,
  33. 7881200, 4581002 };
  34.  
  35. // INPUT Variables
  36. int accountNumber;
  37.  
  38. // Flag to indicate if the number was found
  39. bool found = false;
  40.  
  41. // Get the charge account number from the user
  42. cout << "Enter your charge account number: ";
  43. cin >> accountNumber;
  44.  
  45. // Search the array for the account number
  46. for (int index = 0; index < NUM_ACCOUNTS; index++)
  47. {
  48. if (accounts[index] == accountNumber)
  49. {
  50. found = true;
  51. break;
  52. }
  53. }
  54.  
  55. // Output Result
  56. if (found)
  57. {
  58. cout << "The number is valid." << endl;
  59. }
  60. else
  61. {
  62. cout << "The number is invalid." << endl;
  63. }
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0.01s 5344KB
stdin
5658845
stdout
Enter your charge account number: The number is valid.