fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 7 P. 447 # 13
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Display Lottery Game
  6.  * ____________________________________________________________________________
  7.  * This program will generate a random number in the range of 0 through 9 five
  8.  * times while the user tries to guess what each number is going to be,
  9.  * simulating a lottery. If all numbers match, the user is a grand prize winner!
  10.  * ____________________________________________________________________________
  11.  * Input
  12.  * size //A constant representing the number of digits in the lottery
  13.  * user //Stores the 5 digits enetered by the user
  14.  * Output
  15.  * lottery //Stores the 5 randomly genertae lottery digits
  16.  * matches //Keeps track of how many digits the user entered that matched with the lottery ones
  17.  *****************************************************************************/
  18. #include <iostream>
  19. #include <cstdlib>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. int main() {
  24. //Data Dictionary
  25. const int SIZE = 5; //INPUT - A constant representing the number of digits in the lottery
  26. int user[SIZE]; //INPUT - Stores the 5 digits enetered by the user
  27. int lottery[SIZE]; //OUTPUT -Stores the 5 randomly genertae lottery digits
  28. int matches = 0; //OUTPUT - Keeps track of how many digits the user entered that matched with the lottery ones
  29.  
  30. //Generate Random Lottery Numbers (0-9)
  31. srand(0);
  32. for(int i = 0; i < SIZE; i++){
  33. lottery[i] = rand() % 10;
  34. }
  35. //Start Game
  36. cout << "Welcome to the Lottery Game!" << endl;
  37. cout << "Try to guess the 5 lottery digits (each between 0 and 9)." << endl;
  38. cout << "Enter your 5 digits below" << endl;
  39.  
  40. //Get User's Guesses
  41. for (int i = 0; i < SIZE; i++){
  42. cout << "Digit " << (i + 1) << ": " << endl;
  43. cin >> user[i];
  44. //Input Validation
  45. while(user[i] < 0 || user[i] > 9){
  46. cout << "Invalid input. Please enter a digit between 0 and 9: " << endl;
  47. cin >> user[i];
  48. }
  49. }
  50.  
  51. //Compare
  52. for (int i = 0; i < SIZE; i++){
  53. if(lottery[i] == user[i]){
  54. matches++;
  55. }
  56. }
  57.  
  58. //Reveal Lottery Numbers
  59. cout << "Lottery Results:" << endl;
  60. cout << "Lottery Numbers:";
  61. for(int i = 0; i < SIZE; i++){
  62. cout << lottery[i];
  63. }
  64.  
  65. cout << "\nYour numbers: ";
  66. for (int i = 0; i < SIZE; i++){
  67. cout << user[i];
  68. }
  69.  
  70. //Display Results
  71. cout << "\nMatching digits: " << matches << endl;
  72. if(matches == SIZE)
  73. cout << "CONGRATULATIONS! You're the GRAND PRIZE WINNER!" << endl;
  74. else if (matches > 0)
  75. cout << "Nice try! You matched " << matches << " digits." << endl;
  76. else
  77. cout << "No matches this time. Better luck next time!" << endl;
  78.  
  79. cout << "Thanks for playing!" << endl;
  80. return 0;
  81. }
Success #stdin #stdout 0s 5312KB
stdin
9
8
7
5
4
stdout
Welcome to the Lottery Game!
Try to guess the 5 lottery digits (each between 0 and 9).
Enter your 5 digits below
Digit 1: 
Digit 2: 
Digit 3: 
Digit 4: 
Digit 5: 
Lottery Results:
Lottery Numbers:36753
Your numbers: 98754
Matching digits: 2
Nice try! You matched 2 digits.
Thanks for playing!