fork download
  1. //Devin Scheu CS1A Chapter 8, P. 487, #1
  2. //
  3. /**************************************************************
  4. *
  5. * VALIDATE CHARGE ACCOUNT NUMBER
  6. * ____________________________________________________________
  7. * This program determines if a charge account number is valid
  8. * by checking against a predefined list.
  9. * ____________________________________________________________
  10. * INPUT
  11. * accountNumber : The charge account number entered by the user
  12. *
  13. * OUTPUT
  14. * validationMessage : Message indicating if the number is valid or invalid
  15. *
  16. **************************************************************/
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20.  
  21. using namespace std;
  22.  
  23. int main () {
  24.  
  25. //Variable Declarations
  26. const int NUM_ACCOUNTS = 18; //OUTPUT - Number of valid accounts
  27. long long validAccounts[NUM_ACCOUNTS] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  28. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  29. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  30. long long accountNumber; //INPUT - The charge account number entered by the user
  31. bool isValid = false; //PROCESSING - Flag indicating if the account is valid
  32. string validationMessage; //OUTPUT - Message indicating if the number is valid or invalid
  33.  
  34. //Prompt for Input
  35. cout << "Enter the charge account number: ";
  36. cin >> accountNumber;
  37. cout << accountNumber << endl;
  38.  
  39. //Linear Search for Validation
  40. for (int i = 0; i < NUM_ACCOUNTS; i++) {
  41. if (validAccounts[i] == accountNumber) {
  42. isValid = true;
  43. break;
  44. }
  45. }
  46.  
  47. validationMessage = isValid ? "The number is valid." : "The number is invalid.";
  48.  
  49. //Separator and Output Section
  50. cout << "-------------------------------------------------------" << endl;
  51. cout << "OUTPUT:" << endl;
  52.  
  53. //Output Result
  54. cout << left << setw(25) << "Validation Status:" << right << setw(15) << validationMessage << endl;
  55.  
  56. } //end of main()
Success #stdin #stdout 0.01s 5292KB
stdin
8777541
stdout
Enter the charge account number: 8777541
-------------------------------------------------------
OUTPUT:
Validation Status:       The number is valid.