A lottery with arrays-displaying matching digits.

Pages: 12
I'm almost done with this program. All I need to do is display the number of matching digits by comparing the user's response to the numbers generated by the lottery. The matching digits have to be in order(I.E permutation). Suggestions on approving this program as well as my coding style would be incredibly appreciated.

Edit- Oh, I forgot to mention that this lottery can only have five integers. So there should only five matching digits.

Here's an example where element 2(number 9) and element 4(number 3) are matching digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream> // A library used for basic input and output.
#include <cstdlib> // Has the rand function
#include <ctime> // Has the time function.
#include <cstdio> // Has the NULL function.
using namespace std;

int main()
{
  char response; // a variable to repeat the loop.
  const int SIZE = 5; // The size of the array.
  int lottery[SIZE]; // A variable that will hold random numbers.
  int user[SIZE]; // An array the holds the user's numbers;
  int counter = 1; // A variable that will be used in a loop.
  bool match = true; // A variable that will check for matches.

  cout << "Welcome to Luis' million dollar lottery.\n " << endl;
  cout << "I know you're not going to win, but why not give it a shot.";
  cout << "\n";
  cout << "\nNote: This lottery only accepts numbers from 0 to 9.\n ";

do // A loop that will repeat until the user types N or n
{
  // A loop that stores five numbers in the user array.
 for (counter = 0; counter < SIZE; counter++)
   {
       cout << "\nPlease enter number. " << counter + 1 << ": ";
       cin  >> user[counter];

       // Checking for input validation.
      while(user[counter] < 0 || user[counter] > 9)
    {
     cerr << "\nError, this number is not accepted. ";
     cout << "\n\nPlease enter another number. " << counter + 1 << ": ";
     cin >> user[counter];
    }
   }

  /* Generate a random seed with five numbers and store it in the
  lottery array. */
  srand(time(NULL));

   for(counter = 0; counter < SIZE; counter++)
      {
        lottery[counter] = rand() % 9 + 0;
      }

   // Display the numbers in the lottery array.
   cout << "\nThe winning numbers are: ";

   for(counter = 0; counter < SIZE; counter++)
      {
        cout << " " << lottery[counter];
      }

    // Display the numbers that the user selected.
    cout << "\n\nThe numbers that you selected were: ";


   for(counter = 0; counter < SIZE; counter++)
      {
        cout << " " << user[counter];
      }


    // Check to see if they're matching numbers.
   int total = 0; /* the total number of matches and refreshes the
                     variable back to zero each time the user repeats
                     the loop. */
   for(counter = 0; counter < SIZE; counter++)
      {
        if(user[counter] != lottery[counter])
        {
          match = false;
        }
        else
            total += 1; // Increment 1 each time there is a match.
      }
    cout << "\n\n" << total; // Display the total matches.
    cout << " of the 5 numbers are matching numbers. ";


    if(match) // If both the lottery array and user array match.
         cout << "\n\nYou're are one lucky person.\n";
    else  // If both the lottery array and user array don't match.
         cout << "\n\nSo close but no cigar. Care to try again?[Y/N] ";
         cin >> response;

    // Checking for input validation.
    while(response != 'Y' && response != 'y' &&
          response != 'N' && response != 'n')
    {
        cerr << "\nError, invalid input. ";
        cout << " Please input Y, y, N, or n as your response. ";
        cin >> response;
    }
}
 while(response != 'N' && response != 'n'); /* Repeat until they
 type N or n as their response. */

 return 0;
}

Last edited on
Arrays are indexed starting at 0, all you for loops are wrong.
1
2
3
for (counter = 1; counter <= SIZE; counter++)
// should be
for (counter = 0; counter < SIZE; counter++)

alright thanks will do.
Alright I fixed it.
And it will still crash because of cin >> response[4];
it doesn't crash for me. It works perfectly. If may ask, why do you think it will crash and are you saying that it will crash if I add something else or in its current state. In its current state, it doesn't crash. Also, there are two lines (line 79 & 87) that say the same thing but in a different scope. Which one are you referring to?
Last edited on
That is surprising!
char response[4]={'y','Y','N','n'};
contains 4 elements, after response[3] the array goes out of bounds.
response[0] = 'y'
response[1] = 'Y'
response[2] = 'N'
response[3] = 'n'
response[4] = out of bounds/does not exist
Oh wow, I remember my instructor talking about that. Hmm, let me try 3 instead of 4.

edit. Nope char response[3] causes an error. Looks like char response[4] works but I'm not sure if it causing memory leaks or bugs since that can happen if you go out of bounds.


Error- too many initializers for char[3].
Last edited on
You can change it to
char response[5]={'y','Y','N','n'};
and it will work.
A better solution is to not use an array at all which is basically what adding the extra element does with the way you have it written.

Changing every instance of
1
2
3
response[4];
// to 
response[3];

does the same thing though since neither way actually uses the array, it just takes longer.
Last edited on
Alright will do thanks.
Alright it's fixed. Now, how would I go about in displaying the matching digits considering that the user array has to be exactly like the lottery array(I.E in the same order).

Example

lottery array: 1, 2, 3, 4, 5
user array: 1, 2, 3, 4, 5 Not 5, 4, 3, 2, 1



congratulations you're a winner.
Last edited on
My edit should have said "every other instance of" since as you found out you cannot initialize an array with more elements than they array holds any more than you can add more than it can hold.

Memory leaks are not an issue with static arrays also.
well, regardless I decided to leave response without an array just to make this program a little bit more clean and, of course, it still works. Nevertheless, back to the main task at hand and that is displaying the number of matching digits that appear in order. How should I go about it? maybe another for loop.
You already have a for loop that compares the numbers.
1
2
3
4
5
6
7
8
   // Check to see if they're matching numbers.
   for(counter = 0; counter < SIZE; counter++)
      {
        if(user[counter] != lottery [counter])
        {
          match = false;
        }
      }

The question is more how you want to display them. The simplest way is to add an else that prints the number. I assume it is after you say if they won or lost though and if it is only if they won, you just need to print either array. If you only want to print matches, that means you have to store the matches in another array and print them.
Well, the program already displays the lottery array and the numbers that you inputted(User Array). All I need to do is to make the program say the number of matches that both arrays have.


Since there are only 5 integers, that means if there only 4 or less matches then it should always display (line78) cout << "so close but no cigar. Care to try again. "; unless there are 5 matches.

Example 1: Lottery Array: 1,2,3,4,5 User Array: 1,2,3,4,5 Matches = 5
Example 2: Lottery Array: 1,2,3,4,5 User Array: 5,4,3,2,1 Matches = 0

In the first example above, both arrays are in order. This means that there are 5 matches and should display (line76) cout << "You're one lucky person. ";. I hope that gives you more insight.
Last edited on
That still is just a matter of adding an else:
1
2
else
    matches++;

and outputting "matches" at the right place.
Alright thanks, you have been a big help. I'll see what I can do.
Last edited on
Alright, so I added match++; on line 73 and a cout statement on line 75 that it suppose to display the number of matches but as usual there are a couple of problems.

The first problem is that counts the last number as a match. For example, if the last number in the lottery array is a 5 and your last entry is a 5, it counts. Regardless if the first number in the lottery array was a 5 and you had inputted 5 as your first entry.

The second problem is that if there is just one match, then it outputs the cout statement on line 79 as if you had 5 matches when you didn't.

Those are the only two problems that I found. Any suggestions?
Last edited on
You seem to be just looking someone to fill in the blanks and not listening to what I say.
1
2
3
4
5
6
7
8
9
   for(counter = 0; counter < SIZE; counter++)
      {
        if(user[counter] != lottery[counter])
        {
          match = false;
        }
        else
            match++
      }

Can you not see what is wrong with that? You cannot increment a boolean. Well, you sort of can but, it takes paying a lot more of paying attention and its not what you want in the first place. Look at what I posted, I introduces a new variable.
Well I think I did it. Here is what I did.

1
2
3
4
5
6
7
8
9
10
11
// Check to see if they're matching numbers.
   for(counter = 0; counter < SIZE; counter++)
      {
        if(user[counter] != lottery[counter])
        {
          match = false;
        }
        else
            total += 1;
      }
    cout << "\n\nYour entry has " << total << " matches. ";


Like you said, I introduced a new variable called int total(I don't know why you insisted this new variable to be an array), I set this new variable to 0 and created an operation that will add the value of 1 each time it finds a number that is in the right order(makes sense since there are only 5 integers). This also solved my second problem. What do you think? In any case, have a good day admkrk.
Last edited on
Pages: 12