fork download
  1. //Roman Lara Delgado CSC5 Chapter 7, P. 447, #13
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Simulate Lottery
  6.  * ____________________________________________________________________________
  7.  * This program generates a random set of lottery numbers and compares it
  8.  * with a set of inputted numbers. The program then determines how many numbers
  9.  * match and if it's a winning or losing set of numbers.
  10.  * _____________________________________________________________________________
  11.  * INPUT
  12.  * user[] : lottery numbers
  13.  *
  14.  * OUTPUT
  15.  * lottery[] : random set of lottery numbers
  16.  * digitsMatched : number of matching digits in lottery
  17.  *
  18.  * Winning or Losing Outcome of Lottery Ticket
  19.  *
  20.  ******************************************************************************/
  21. #include <iostream>
  22. #include <cstdlib>
  23. #include <ctime>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. /***************************************************************************
  29. * CONSTANTS
  30. * -------------------------------------------------------------------------
  31. * SIZE : Number of digits in lottery number
  32. * ************************************************************************/
  33. //Initialize Program Constants
  34. const int SIZE = 5;
  35.  
  36. //Delcare Program Variables
  37. int user[SIZE]; //INPUT - lottery numbers
  38. int count; //COUNTER - loop counter
  39. int randomNum; //CALC - Random generated
  40. int lottery[SIZE]; //OUTPUT - random winning lottery number
  41. int digitsMatched = 0; //OUTPUT - number of matching digits in lottery
  42.  
  43. //Get System Time
  44. unsigned seed = time(0);
  45.  
  46. //Seed Random Number Generator
  47. srand(seed);
  48.  
  49. //Generate Lottery Ticket Numbers
  50. for (count = 0; count < SIZE; count++)
  51. {
  52. randomNum = 1 + rand() % 9;
  53. lottery[count] = randomNum;
  54. }
  55.  
  56. //Input Lottery Ticket Numbers
  57. for (count = 0; count < SIZE; count++)
  58. {
  59. cout << "Enter Digit #" << count + 1 << ": " << endl;
  60. cin >> user[count];
  61. }
  62.  
  63. //Compare Lottery Ticket Numbers to Winning Numbers
  64. for (count = 0; count < SIZE; count++)
  65. {
  66. if (lottery[count] == user[count])
  67. {
  68. digitsMatched++; //Increments counter variable if true
  69. }
  70. }
  71.  
  72. //Output Lottery Results
  73. cout << endl;
  74. cout << "Lottery Numbers: ";
  75.  
  76. //Output Digits Matching with Lottery
  77. for (count = 0; count < SIZE; count++)
  78. {
  79. cout << lottery[count] << " ";
  80. }
  81. cout << endl;
  82. cout << "Digits Matched: " << digitsMatched;
  83.  
  84. //Output Win or Lose
  85. if (digitsMatched == 5)
  86. cout << "\n!! Congrats you're a winner !!";
  87. else
  88. cout << "\nSorry, better luck next time.";
  89.  
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0.01s 5292KB
stdin
3
7
8
3
6
stdout
Enter Digit #1: 
Enter Digit #2: 
Enter Digit #3: 
Enter Digit #4: 
Enter Digit #5: 

Lottery Numbers: 9 4 8 2 7 
Digits Matched: 1
Sorry, better luck next time.