fork download
  1. // Torrez, Elaine CS1A P. 447, #13
  2.  
  3. // LOTTERY APPLICATION
  4. // *****************************************************************************************
  5. // * This program simulates a simple lottery game. It generates five random numbers *
  6. // * (0–9) and asks the user to enter five guesses. The program compares the user’s *
  7. // * numbers to the lottery numbers and counts how many match. If all match, the user *
  8. // * wins the grand prize. *
  9. // * *
  10. // * INPUT: *
  11. // * user[] : The five numbers entered by the user (0–9) *
  12. // * *
  13. // * OUTPUT: *
  14. // * lottery[]: The five randomly generated lottery numbers (0–9) *
  15. // * matches : The number of positions where user and lottery numbers match *
  16. // * message : Whether the user wins or not *
  17. // *****************************************************************************************
  18. #include <iostream>
  19. #include <cstdlib> // for rand() and srand()
  20. #include <ctime> // for time() to seed random numbers
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. const int SIZE = 5;
  26. int lottery[SIZE]; // stores lottery numbers
  27. int user[SIZE]; // stores user guesses
  28. int matches = 0; // counter for matching digits
  29.  
  30. // Seed random number generator
  31. srand(time(0));
  32.  
  33. // Generate random lottery numbers (0–9)
  34. for (int i = 0; i < SIZE; i++)
  35. {
  36. lottery[i] = rand() % 10;
  37. }
  38.  
  39. // Get user's lottery numbers
  40. cout << "Enter your 5 lottery numbers (each between 0 and 9):\n";
  41. for (int i = 0; i < SIZE; i++)
  42. {
  43. cout << "Number " << (i + 1) << ": ";
  44. cin >> user[i];
  45.  
  46. // Input validation
  47. while (user[i] < 0 || user[i] > 9)
  48. {
  49. cout << "Please enter a number between 0 and 9: ";
  50. cin >> user[i];
  51. }
  52. }
  53.  
  54. // Compare arrays
  55. for (int i = 0; i < SIZE; i++)
  56. {
  57. if (user[i] == lottery[i])
  58. matches++;
  59. }
  60.  
  61. // Display both arrays
  62. cout << "\nLottery Numbers: ";
  63. for (int i = 0; i < SIZE; i++)
  64. cout << lottery[i] << " ";
  65.  
  66. cout << "\nYour Numbers: ";
  67. for (int i = 0; i < SIZE; i++)
  68. cout << user[i] << " ";
  69.  
  70. // Display results
  71. cout << "\n\nYou matched " << matches << " number(s)." << endl;
  72.  
  73. if (matches == SIZE)
  74. cout << " GRAND PRIZE WINNER! All numbers match! \n";
  75. else
  76. cout << "Better luck next time!\n";
  77.  
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0.01s 5292KB
stdin
7
3
5
1
9
stdout
Enter your 5 lottery numbers (each between 0 and 9):
Number 1: Number 2: Number 3: Number 4: Number 5: 
Lottery Numbers: 4 9 7 0 5 
Your Numbers:    7 3 5 1 9 

You matched 0 number(s).
Better luck next time!