fork download
  1. // Timothy Price CS1A Ch 8 Challenge 2
  2. /******************************************************************************
  3.  * Determine lottery winer
  4.  *
  5.  * This program receives a value with a number of digits and determines if
  6.  * that value is within a preset list of values.
  7.  *
  8.  * Input:
  9.  * Lottery number
  10.  * Output:
  11.  * Win/loss identifier
  12.  *****************************************************************************/
  13.  
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. // constant value for number of lottery tickets
  18. const int SIZE = 10;
  19.  
  20. int main() {
  21.  
  22. // data dictionary
  23. int lotteryNumbers[SIZE] = { // Lottery numbers purchased weekly
  24. 13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121
  25. };
  26. int winner; // Winning number
  27. const int maxNumber = 99999; // Values for input validation, to only
  28. const int minNumber = 10000; // accept a 5 digit number.
  29. bool isValid = false; // Boolean value for successful match
  30.  
  31. // Receive input value
  32. cout << "Enter the 5 digit winning lottery number: ";
  33. cin >> winner;
  34. cout << winner << endl;
  35.  
  36. // Input validation
  37. if (winner < minNumber || winner > maxNumber) {
  38. cout << "Invalid entry, enter a 5 digit number: ";
  39. cin >> winner;
  40. cout << winner << endl;
  41. }
  42.  
  43. // Linear search
  44. for (int i = 0; i < SIZE; i++) {
  45. if (lotteryNumbers[i] == winner) {
  46. isValid = true;
  47. break;
  48. }
  49. }
  50.  
  51. // Display output
  52. if (isValid) {
  53. cout << "You have won this week!" << endl;
  54. }
  55. else {
  56. cout << "You did not win this week, sorry." << endl;
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 5332KB
stdin
1357
13579
stdout
Enter the 5 digit winning lottery number: 1357
Invalid entry, enter a 5 digit number: 13579
You have won this week!