### Lottery Simulator Homework Problem

Hey all, I've been working on some code for a homework assignment in my Computer Science I class, and for some reason the next function simply won't run. I'm completely stumped at this point, I have no idea what to change to make it run properly.

The assignment is as follows:

 Write a program that simulates a lottery. The program should have an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element in the array. The user should enter five digits which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4). lottery array: 5 2 6 9 1 user array: 1 3 6 8 1 The program should display the random numbers stored in the lottery array and the number of digits matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.

There are some additional requirements from the teacher, including having specific functions, which is why my code is perhaps more cumbersome than necessary. Here is what I have so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include // for cin and cout streams #include // for the rand and srand functions #include // for the time function //======================================================================================= // Constant Declarations // --------------------- const int lotteryDigits = 9; const int SIZE = 5; //======================================================================================= // Function Prototypes // ------------------- void generateLottery(int[], int, int); void userInput(int[], int); int matchCounter(int[], int[], int); void displayNumbers(int[], int[]); void winnerOrLoser(int); //======================================================================================= // ------------- // Main Function // ------------- //======================================================================================= using namespace std; int main() { // Variable Declarations int lottery[5] = {0, 0, 0, 0, 0}; int user[5] = {0, 0, 0, 0, 0}; int matches = 0; //Function Calls generateLottery(lottery, SIZE, lotteryDigits); userInput(user, SIZE); matchCounter(lottery, user, matches); displayNumbers(lottery, user); winnerOrLoser(matches); system("pause"); return 0; } //end main //======================================================================================= // -------------------- // Function Definitions // -------------------- //======================================================================================= // Randomly generates winning lottery numbers void generateLottery(int lottery[], int, int) { unsigned seed = time(0); srand(seed); for (int count=0; count "; cin >> user[count1]; while (user[count1]<0 || user[count1]>9) { cout << "Error! Entry must be between 0 and 9:---> "; cin >> user[count1]; } } } // end userInput //======================================================================================= // Counts the number of matches int matchCounter(int[], int[], int) { int check = 1; int match = 0; int lotto[5]; int input[5]; while (check <= SIZE) { if (lotto[check] != input[check]) check++; else match = match + 1; } return match; } // end matchCounter //======================================================================================= // Diplays the winning numbers and the user's numbers void displayNumbers(int lottery[], int user[]) { cout << "The winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl; cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl; } // end displayNumbers //======================================================================================= //Displays the number of matches and whether or not the user has won void winnerOrLoser(int matches) { cout << "You matched " << matches << " numbers"; if (matches = SIZE) cout << "Congratulations--you WIN!!!!"; else cout << "Sorry--you lose. Better luck next time."; } // end winnerOrLoser //======================================================================================= ``````

When I go to run this code, it simply stops working after userInput runs. Nothing happens, not even the usual "Press any key to continue..." Just a blinking cursor. I can't figure out why the next function refuses to happen. There are no build errors, and it looks right as far as I can tell, though I am just starting out in learning C++. If any of you could help me identify and fix the problem(s), I would GREATLY appreciate it.

Thanks so much.

PS: It is formatted the way it is for class, so that can't change. Not that it should really matter, I suppose.
There is a problem with your matchCounter function. I don't know if that's what is causing the problem, but you should still fix it.

Right now, there is a possibility that it is entering an infinite loop if any one of the numbers match. What you describe sounds like an infinite loop.

Another problem with the function is that you're declaring the lotto and input arrays in the function. Aren't you supposed to be comparing the arrays that are passed to the function instead?
Ok, so I fixed the infinite loop, and got rid of the declarations inside the function. But now matches don't register... It always displays 0 matches even if some numbers match. I'm not sure what I'm doing wrong. This function seems to be the crux of my problems. Perhaps someone could be so kind as to write out how they would do it and explain why? Please remember that I am only just learning how to do this.

Here's what I have now:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141`` ``````#include // for cin and cout streams #include // for the rand and srand functions #include // for the time function //======================================================================================= // Constant Declarations // --------------------- const int lotteryDigits = 9; const int SIZE = 5; //======================================================================================= // Function Prototypes // ------------------- void generateLottery(int[], int, int); void userInput(int[], int); int matchCounter(int[], int[], int); void displayNumbers(int[], int[]); void winnerOrLoser(int); //======================================================================================= // ------------- // Main Function // ------------- //======================================================================================= using namespace std; int main() { // Variable Declarations int lottery[5] = {0, 0, 0, 0, 0}; int user[5] = {0, 0, 0, 0, 0}; int matches = 0; //Function Calls generateLottery(lottery, SIZE, lotteryDigits); userInput(user, SIZE); matchCounter(lottery, user, matches); displayNumbers(lottery, user); winnerOrLoser(matches); system("pause"); return 0; } //end main //======================================================================================= // -------------------- // Function Definitions // -------------------- //======================================================================================= // Randomly generates winning lottery numbers void generateLottery(int lottery[], int, int) { unsigned seed = time(0); srand(seed); for (int count=0; count "; cin >> user[count1]; while (user[count1]<0 || user[count1]>9) { cout << "Error! Entry must be between 0 and 9:---> "; cin >> user[count1]; } } } // end userInput //======================================================================================= // Counts the number of matches int matchCounter(int lotto[], int input[], int match) { bool numMatch = true; for (int check = 0; check <= SIZE; check++) { if (lotto[check] != input[check]) numMatch = false; check++; } return match; } // end matchCounter //======================================================================================= // Diplays the winning numbers and the user's numbers void displayNumbers(int lottery[], int user[]) { cout << "\nThe winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl; cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl; } // end displayNumbers //======================================================================================= //Displays the number of matches and whether or not the user has won void winnerOrLoser(int matches) { cout << "You matched " << matches << " numbers"; if (matches != SIZE) cout << "\nSorry--you lose. Better luck next time.\n"; else cout << "\nCongratulations--you WIN!!!!\n"; } // end winnerOrLoser //======================================================================================= ``````

Thanks again.
Bumping for great homework completion
Several problems in your code :

1) Your matchCounter() function simply doesn't count the number of matches.
It just checks for a perfect match. But does nothing with the result.

2) It returns match, which is its unmodified input parameter.

3) The return value of the function is not saved. So winnerOrLoser() uses the value assigned in main(), ie 0.

4) Also, matchCounter() reads lotto[SIZE] and input[SIZE], which it shouldn't.

5) Functions userInput() and void generateLottery() are missing arguments.

At least you need to rewrite matchCounter() so that it effectively counts and returns the number of matches and make sure that winnerOrLoser() uses the returned value.
Last edited on
Alright, I made the changes you suggested (or at least I think I did), but the match counter still won't increase. Everything else seems to be okay, but for some reason the match counter simply won't increase. I don't understand what I'm doing wrong.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140`` ``````//======================================================================================= // Constant Declarations // --------------------- const int lotteryDigits = 10; const int SIZE = 5; //======================================================================================= // Function Prototypes // ------------------- int generateLottery(int[], int, int); int userInput(int[], int); int matchCounter(int[], int[], int); void displayNumbers(int[], int[]); void winnerOrLoser(int); //======================================================================================= // ------------- // Main Function // ------------- //======================================================================================= using namespace std; int main() { // Variable Declarations int lottery[5] = {0, 0, 0, 0, 0}; int user[5] = {0, 0, 0, 0, 0}; int matches = 0; //Function Calls generateLottery(lottery, SIZE, lotteryDigits); userInput(user, SIZE); matchCounter(lottery, user, matches); displayNumbers(lottery, user); winnerOrLoser(matches); system("pause"); return 0; } //end main //======================================================================================= // -------------------- // Function Definitions // -------------------- //======================================================================================= // Randomly generates winning lottery numbers int generateLottery(int lottery[], int, int) { unsigned seed = time(0); srand(seed); for (int y=0; y "; cin >> user[y]; while (user[y]<0 || user[y]>9) { cout << "Error! Entry must be between 0 and 9:---> "; cin >> user[y]; } } return user[0], user[1], user[2], user[3], user[4]; } // end userInput //======================================================================================= // Counts the number of matches int matchCounter(int lotto[], int input[], int) { int match = 0; for (int x = 0; x < SIZE; ++x) { if (lotto[x] == input[x]) match = match + 1; } return match; } // end matchCounter //======================================================================================= // Diplays the winning numbers and the user's numbers void displayNumbers(int lottery[], int user[]) { cout << "\nThe winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl; cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl; } // end displayNumbers //======================================================================================= //Displays the number of matches and whether or not the user has won void winnerOrLoser(int matches) { cout << "You matched " << matches << " numbers"; if (matches != SIZE) cout << "\nSorry--you lose. Better luck next time.\n"; else cout << "\nCongratulations--you WIN!!!!\n"; } // end winnerOrLoser //======================================================================================= ``````
It's problem 3).

Replace line 42 with
 `` `` ``matches = matchCounter(lottery, user, matches);``

Replace line 85 with this:

printf( "\nEnter digit # %d of 5 Between 0 and 9:---> ", y+1 ) ;

It's good that the user input is being accepted
and it's good to know what to expect.

btw, I hate using that "5" like that, hard coded, as it really "Should" be another var, but, hey, that's another matter.
Last edited on
Topic archived. No new replies allowed.