fork download
  1. //Devin Scheu CS1A Chapter 7, P. 447, #13
  2. //
  3. /**************************************************************
  4. *
  5. * SIMULATE LOTTERY AND CHECK MATCHING DIGITS
  6. * ____________________________________________________________
  7. * This program determines the number of matching digits
  8. * between a randomly generated lottery array and a user array.
  9. * ____________________________________________________________
  10. * INPUT
  11. * userNumbers : The five digits entered by the user
  12. *
  13. * OUTPUT
  14. * lotteryNumbers : The randomly generated lottery array
  15. * matchCount : The number of matching digits
  16. * winMessage : Message proclaiming a grand prize winner if all digits match
  17. *
  18. **************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <cstdlib>
  23. #include <ctime>
  24.  
  25. using namespace std;
  26.  
  27. int main () {
  28.  
  29. //Variable Declarations
  30. const int NUM_DIGITS = 5; //OUTPUT - Number of digits in the lottery
  31. int lotteryNumbers[NUM_DIGITS]; //OUTPUT - The randomly generated lottery array
  32. int userNumbers[NUM_DIGITS]; //INPUT - The five digits entered by the user
  33. int matchCount = 0; //OUTPUT - The number of matching digits
  34. string winMessage; //OUTPUT - Message proclaiming a grand prize winner if all digits match
  35.  
  36. //Seed the random number generator
  37. srand(static_cast<unsigned int>(time(0)));
  38.  
  39. //Generate Random Lottery Numbers
  40. for (int i = 0; i < NUM_DIGITS; i++) {
  41. lotteryNumbers[i] = rand() % 10; // Random number 0 through 9
  42. }
  43.  
  44. //Prompt for Input
  45. for (int i = 0; i < NUM_DIGITS; i++) {
  46. cout << "Enter digit " << (i + 1) << " (0-9): ";
  47. cin >> userNumbers[i];
  48. while (userNumbers[i] < 0 || userNumbers[i] > 9) {
  49. cout << "\nError: Please enter a number between 0 and 9: ";
  50. cin >> userNumbers[i];
  51. }
  52. cout << userNumbers[i] << endl;
  53. }
  54.  
  55. //Compare Arrays for Matches
  56. for (int i = 0; i < NUM_DIGITS; i++) {
  57. if (userNumbers[i] == lotteryNumbers[i]) {
  58. matchCount++;
  59. }
  60. }
  61.  
  62. //Determine Win Status
  63. winMessage = (matchCount == NUM_DIGITS) ? "Congratulations! You are a grand prize winner!" : "";
  64.  
  65. //Separator and Output Section
  66. cout << "-------------------------------------------------------" << endl;
  67. cout << "OUTPUT:" << endl;
  68.  
  69. //Output Result
  70. cout << left << setw(25) << "Lottery Numbers:" << right;
  71. for (int i = 0; i < NUM_DIGITS; i++) {
  72. cout << lotteryNumbers[i];
  73. if (i < NUM_DIGITS - 1) cout << ", ";
  74. }
  75. cout << endl;
  76.  
  77. cout << left << setw(25) << "Matching Digits:" << right << setw(15) << matchCount << endl;
  78. if (!winMessage.empty()) {
  79. cout << left << setw(25) << "Message:" << right << setw(15) << winMessage << endl;
  80. }
  81.  
  82. } //end of main()
Success #stdin #stdout 0.01s 5324KB
stdin
1
3
6
7
3
stdout
Enter digit 1 (0-9): 1
Enter digit 2 (0-9): 3
Enter digit 3 (0-9): 6
Enter digit 4 (0-9): 7
Enter digit 5 (0-9): 3
-------------------------------------------------------
OUTPUT:
Lottery Numbers:         6, 2, 4, 7, 5
Matching Digits:                       1