fork download
  1. //Nathan Dominguez CSC5 Chapter 8, P.487, #1
  2. //
  3. /*******************************************************************************
  4. *
  5. * Validate an Account Charge with a Linear Search
  6. *_______________________________________________________________________________
  7. * This program prompts the user to enter a charge account number, the program
  8. * then uses a linear seach method to determine if the entered number is a valid
  9. * account charge number.
  10. *_______________________________________________________________________________
  11. * INPUT
  12. * number : the input number
  13. * acc_num[] : the array holds 18 charge account numbers
  14. * OUTPUT
  15. *
  16. * result : the variable stores the values 0-17 and -1 from the
  17. * LinearSearch function.
  18. * LinearSearch : it is a function that returns 0-17 or -1. If this function
  19. * returns 0-17 then the user's input is a winning ticket, if
  20. * it returns -1 then the user's input is not a winning ticket.
  21. * index : stores the index number of the acc_num[] array in the
  22. * LinearSearch() function.
  23. * position : the variable returns the position of the user's number. If
  24. * position = -1 then the user's number was not found
  25. *
  26. *******************************************************************************/
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. //LinearSearch() prototype
  31. int LinearSearch(int array[], int size, int number);
  32.  
  33. int main()
  34. {
  35. //CONSTANT - array with 18 valid account charge numbers
  36. int acc_num[] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  37. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  38. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  39.  
  40. int number;
  41. //
  42. //user input variable
  43. cout << "Enter your charge account number: ";
  44. cin >> number;
  45. //
  46. //Input Validation
  47. while (number < 0 || number > 9999999)
  48. {
  49. cout << number << endl;
  50. cout << "Not a valid entry, please enter a positive 7-digit number: ";
  51. cin >> number;
  52. }
  53. cout << number << endl;
  54.  
  55. //setting a variable to hold the LinearSearch output
  56. int result = LinearSearch(acc_num, 18, number);
  57.  
  58. //VALIDATE
  59. if (result >= 0)
  60. {
  61. cout << number << " is a valid number." << endl;
  62. }
  63. else
  64. {
  65. cout << number << " is not a valid number." << endl;
  66. }
  67. return 0;
  68. }
  69. //
  70. //LinearSearch function definition
  71. int LinearSearch(int array[], int size, int number)
  72. {
  73. int index = 0;
  74. int position = -1;
  75. bool found = false;
  76. //
  77. //LOOP - find user input
  78. while (index < size && !found)
  79. {
  80. if (array[index] == number)
  81. {
  82. found = true;
  83. position = index;
  84. }
  85. index++;
  86. }
  87. return position;
  88. }
Success #stdin #stdout 0.01s 5304KB
stdin
5658845
stdout
Enter your charge account number: 5658845
5658845 is a valid number.