fork download
  1. //Maxwell Brewer CS1A Chapter 7, p. 447, #13
  2. //
  3. /*******************************************************************************
  4.  * LOTTERY GAME
  5.  * _____________________________________________________________________________
  6.  * This program will generate a 5-digit lottery ticket and the user wins if
  7.  * the ticket number matches.
  8.  * _____________________________________________________________________________
  9.  * INPUTS
  10.  * lottery
  11.  * user
  12.  *
  13.  * OUTPUTS
  14.  * matching
  15.  *
  16.  * ****************************************************************************/
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. int main() {
  22. // Declare variables for the lottery and user tickets
  23. int lottery[5];
  24. int user[5];
  25. int matching = 0; // Variable to keep track of matching digits
  26.  
  27. // Seed the random number generator
  28. srand(time(0));
  29.  
  30. // Populate lottery array with random values from 0 to 9
  31. for (int counter = 0; counter < 5; counter++) {
  32. lottery[counter] = rand() % 10;
  33. }
  34.  
  35. // Get ticket numbers from the user
  36. cout << "Enter your lottery numbers separated by spaces [0-9]:\n";
  37. for (int counter = 0; counter < 5; counter++) {
  38. cin >> user[counter];
  39.  
  40. // Count matching digits as values are read into the user array
  41. if (user[counter] == lottery[counter]) {
  42. matching++;
  43. }
  44. }
  45.  
  46. // Display lottery ticket
  47. cout << "\nHere is the winning ticket:\n";
  48. for (int counter = 0; counter < 5; counter++) {
  49. cout << lottery[counter] << " ";
  50. }
  51.  
  52. // Display winning message or number of matching digits
  53. if (matching == 5) {
  54. cout << "\n\nCongratulations! You had the winning numbers! You are the grand prize winner!\n";
  55. } else {
  56. cout << "\n\nYou only had " << matching << " matching numbers!\n";
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty