fork download
  1. //Roman Lara Delgado CSC5 Chapter 8, P.487, #1
  2. //
  3. /*******************************************************************************
  4. *
  5. * Determine Invalid Account Number
  6. *_______________________________________________________________________________
  7. * This program determines if an account number is invalid meaning it does not
  8. * exist in a set of account numbers.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * accNumber_user : Account number entered by the user.
  12. *
  13. * OUTPUT
  14. * A message that says whether the account number entered
  15. * is valid or not.
  16. * ******************************************************************************
  17. * FUNCTION
  18. * ******************************************************************************
  19. * int searchList(const int [], int, int)
  20. * ------------------------------------------------------------------------------
  21. * This function locates if a value exists in an array.
  22. *
  23. * PARAMETERS:
  24. * array[] : receives the values of an array.
  25. * size : receives the number of elements
  26. * of the array.
  27. * value : receives a value that the user
  28. * wanted to determine whether the
  29. * value exists in the array or not.
  30. *
  31. *******************************************************************************/
  32. #include <iostream>
  33. using namespace std;
  34.  
  35. int searchList(const int [], int, int); //Function prototype
  36. const int SIZE = 18; //GLOBAL VARIABLE - Number of elements of an array.
  37.  
  38. int main () //Start main
  39. {
  40. //Array that stores the account numbers of the data base.
  41. int accNumber_saved[SIZE] = {5658845, 8080152, 1005231, 4520125, 4562555,
  42. 6545231, 7895122, 5552012, 3852085, 8777541,
  43. 5050552, 7576651, 8451277, 7825877, 7881200,
  44. 1302850, 1250255, 4581002};
  45. int accNumber_user; //INPUT - Account number entered by the user.
  46. int results; //Variable that determines if a number is valid or not.
  47.  
  48. //Enter the account number by the user.
  49. cout << "Enter your account number: " << endl;
  50. cin >> accNumber_user;
  51.  
  52. //Assign a value to results by calling the function searchList
  53. results = searchList(accNumber_saved, SIZE, accNumber_user);
  54.  
  55. //if results is -1, then the number is invalid.
  56. if(results == -1)
  57. {
  58. cout << "The number is invalid.";
  59. }
  60. else //Otherwise is valid.
  61. {
  62. cout << "The number is valid.";
  63. }
  64. return 0;
  65. } //End main
  66.  
  67. //searchList definition
  68. int searchList(const int array[], int size, int value) //Start seachList
  69. {
  70. int index = 0; //subscript of the array
  71. int position = -1; //To record position of search value
  72. bool found = false; //Flag to indicate if the value was found
  73.  
  74. while (index < size && !found)
  75. {
  76. if (array[index] == value) //If the value us found
  77. {
  78. found = true; //Set the flag
  79. position = index; //Record the value's subscript
  80. }
  81. index++; //Go to the next element
  82. }
  83. return position; //Return the position, or -1.
  84. } //End searchList
  85.  
Success #stdin #stdout 0.01s 5288KB
stdin
2635172
stdout
Enter your account number: 
The number is invalid.