fork download
  1. //Ryan Shahriyarpour CS1A Carl Argila CH7 HW Q:13
  2. /*****************************************************************************
  3.  *
  4.  * Lottery Application
  5.  *
  6.  *********************
  7.  *
  8.  * This program simulates a lottery. The array lottery holds 5 random digits
  9.  * (0 - 9). The user enters 5 digits into the array user. The program counts
  10.  * how many digits match in the same position and displays the lottery
  11.  * numbers. If all 5 match, the user is the grand prize winner.
  12.  *
  13.  * INPUT
  14.  * Five digits from the user
  15.  *
  16.  * OUTPUT
  17.  * The lottery digits, the number of matches, and a grand prize message
  18.  ****************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <cstdlib>
  22. #include <ctime>
  23. using namespace std;
  24.  
  25. const int SIZE = 5;
  26.  
  27. int main()
  28. {
  29. int lottery[SIZE];
  30. int user[SIZE];
  31. int matches = 0;
  32.  
  33. // Make the random lottery digits
  34. srand(time(0));
  35. for (int i = 0; i < SIZE; i++)
  36. lottery[i] = rand() % 10;
  37.  
  38. // Input (only 0 - 9 allowed)
  39. for (int i = 0; i < SIZE; i++)
  40. {
  41. cout << "Enter digit " << i + 1 << " (0-9): ";
  42. cin >> user[i];
  43. while (user[i] < 0 || user[i] > 9)
  44. {
  45. cout << "Must be 0 to 9. Re-enter: ";
  46. cin >> user[i];
  47. }
  48. }
  49.  
  50. // Count the matches
  51. for (int i = 0; i < SIZE; i++)
  52. {
  53. if (lottery[i] == user[i])
  54. matches++;
  55. }
  56.  
  57. // Output
  58. cout << "\nLottery numbers: ";
  59. for (int i = 0; i < SIZE; i++)
  60. cout << lottery[i] << " ";
  61. cout << "\nMatching digits: " << matches << endl;
  62.  
  63. if (matches == SIZE)
  64. cout << "GRAND PRIZE WINNER!" << endl;
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 5320KB
stdin
2
8
6
8
9
stdout
Enter digit 1 (0-9): Enter digit 2 (0-9): Enter digit 3 (0-9): Enter digit 4 (0-9): Enter digit 5 (0-9): 
Lottery numbers: 9 5 5 3 0 
Matching digits: 0