One glitch in my program

I am able to do the following program with limitations. I am not able to successfully compare the array with lottery numbers and the array with player numbers especially when a number appears more than one time in either arrays.
Please help???????

Program:
Your state is in a process of creating a weekly lottery. Once a week, five
distinct random integers between 1 to 40 (inclusive) are drawn. If a player
guesses all of the numbers correctly, the player wins a certain amount.
Write a program that does the following:
a. Generates five distinct random numbers between 1 and 40 (inclusive)
and stores them in an array.
b. Sorts the array containing the lottery numbers.
c. Prompts the player to select five distinct integers between 1 and 40 (inclusive)
and stores the numbers in an array. The player can select the numbers
in any order, and the array containing the numbers need not be sorted.
d. Determines whether the player guessed the lottery numbers correctly. If
the player guessed the lottery numbers correctly, it outputs the message
"You win!"; otherwise it outputs the message "You lose!" and outputs
the lottery numbers.
Your program should allow a player to play the game as many times as the
player wants to play. Before each play, generate a new set of lottery
numbers.
Post your code and tell us about the portion of it you are having problems with, not just that you can't make it do X.
Line 49 to Line 65 is what i think i am not doing correct.

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
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

const int LOTTERY_NUMBERS = 5;

void sortRandomLottoNumbers(int list[], int listLength);

int main()
{
    int lottoNum[LOTTERY_NUMBERS];
    int playerNum[LOTTERY_NUMBERS];
    int i, j;
    char decide;
    //bool winner;
    int counter;

    cout << "********** LOTTERY GAME **********" << endl << endl;

    // Asks the player whether or not he or she wants to play lottery.
    cout << "Do you want to play lottery (Enter Y for Yes and N for No): ";
    cin >> decide;
    cout << endl;

    while (decide == 'Y' || decide == 'y')
    {
        srand(time(0));
        cout << "Generated lottery numbers: ";
        for (i=0; i<LOTTERY_NUMBERS; i++)
            lottoNum[i] = rand() % 39 + 1;
            // Used 39 instead of 40 because we are adding 1.
            // We are adding 1 because a random number can also be 0,
            // and we don't want to include 0 as a lottery number.

        for (i=0; i<LOTTERY_NUMBERS; i++)
            cout << lottoNum[i] << " ";
        cout << endl << endl;
        // Here lottery numbers are being outputted to use them for candidate input to
        // check the validity of program for a winning candidate.

        sortRandomLottoNumbers(lottoNum, LOTTERY_NUMBERS);  // Sorts the lottery numbers.

        cout << "Pick 5 numbers between 1 & 40: ";          // Asks the user to pick lottery numbers.
        for (i=0; i<LOTTERY_NUMBERS; i++)
            cin >> playerNum[i];
        cout << endl;

        counter=0;
        for (i=0; i<LOTTERY_NUMBERS; i++)
        {
            for (int j=0; j<LOTTERY_NUMBERS; j++)
                if (playerNum[i] == lottoNum[j])
                    counter++;
        }

        if (counter==5)
            cout << "You Win !" << endl << endl;
        else
        {
            cout << "You Lose !" << endl;
            cout << "The winning numbers are: ";
            for (i=0; i<LOTTERY_NUMBERS; i++)
                cout << lottoNum[i] << " ";
            cout << endl << endl;
        }

        cout << "Do you want to play lottery (Enter Y for Yes and N for No): ";
        cin >> decide;
        cout << endl;
    }

    return 0;
}

void sortRandomLottoNumbers(int list[], int listLength)
{
    int index;
    int temp;
    int location;
    int smallIndex;

    for (index=0; index<listLength-1; index++)
    {
        smallIndex = index;
        for (location=index+1; location<listLength; location++)
            if (list[location] < list[smallIndex])
                smallIndex = location;

            temp = list[smallIndex];
            list[smallIndex] = list[index];
            list[index] = temp;
    }

    cout << "After sorting lottery numbers are:" << endl;
    for (index=0; index<listLength; index++)
        cout << list[index] << " ";
    cout << endl << endl;
}
int j is not being used here, you did know that... correct?
Last edited on
Yes. you are correct. I removed it. Thanks.
Topic archived. No new replies allowed.