fork download
  1. // Torrez, Elaine CS1A Chapter 8 P. 487, #2
  2. /********************************************************************************************
  3.  *
  4.  * DETERMINE LOTTERY WINNER
  5.  *
  6.  * ------------------------------------------------------------------------------------------
  7.  * This program determines whether any of a buyer's 10 lottery tickets is a winner.
  8.  * The program stores a fixed list of 10 "lucky" 5-digit combinations in an integer
  9.  * array. It then asks the user to enter this week's winning 5-digit number and
  10.  * performs a linear search through the array to see if any ticket matches.
  11.  * If a match is found, the program reports that the player has a winning ticket;
  12.  * otherwise, it reports that none of the tickets is a winner this week.
  13.  * ------------------------------------------------------------------------------------------
  14.  *
  15.  * INPUT
  16.  * winningNum : This week's winning 5-digit lottery number
  17.  *
  18.  * OUTPUT
  19.  * Message stating whether the player has a winning ticket or not
  20.  *
  21.  ********************************************************************************************/
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. int main()
  28. {
  29. const int SIZE = 10; // Number of lottery tickets
  30.  
  31. int tickets[SIZE] = { // Buyer's 10 "lucky" numbers
  32. 13579, 26791, 26792, 33445, 55555,
  33. 62483, 77777, 79422, 85647, 93121
  34. };
  35.  
  36. int winningNum; // This week's winning number
  37. bool isWinner = false; // True if a ticket matches
  38.  
  39. // ----------------------------------------------------
  40. // INPUT: Get this week's winning 5-digit number
  41. // ----------------------------------------------------
  42. cout << "Enter this week's winning 5-digit lottery number: ";
  43. cin >> winningNum;
  44.  
  45. // Simple input validation for a 5-digit positive number
  46. while (winningNum < 10000 || winningNum > 99999)
  47. {
  48. cout << "ERROR: Enter a VALID 5-digit number (10000 - 99999): ";
  49. cin >> winningNum;
  50. }
  51.  
  52. // ----------------------------------------------------
  53. // PROCESSING: Linear search through the ticket list
  54. // ----------------------------------------------------
  55. for (int i = 0; i < SIZE; i++)
  56. {
  57. if (tickets[i] == winningNum)
  58. {
  59. isWinner = true;
  60. break; // Exit loop early if match found
  61. }
  62. }
  63.  
  64. cout << endl;
  65. cout << fixed << setprecision(0);
  66.  
  67. // ----------------------------------------------------
  68. // OUTPUT: Report if any ticket is a winner
  69. // ----------------------------------------------------
  70. if (isWinner)
  71. cout << "Congratulations! One of your tickets is a WINNER." << endl;
  72. else
  73. cout << "Sorry, none of your tickets is a winner this week." << endl;
  74.  
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5276KB
stdin
13579
stdout
Enter this week's winning 5-digit lottery number: 
Congratulations! One of your tickets is a WINNER.