Please help urgent, late with homework

Hi I have wrote this for a homework, but can't get it to work.
Not allowed to use other libraries than those I used so you know.


Anyways the problem is that now it just gives me an infinite loop of what looks like the same number, but it goes so fast I couldn't really see. So it's not randomizing good. Please Help I was really due to be done with this 6 days ago, but haven't gotten any time because of stress unrelated to school. I am still stressed and can't focus enough to get it to work:/



WHAT HAVE I DONE WRONG?



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
// The program draws 7 random "lottery"numbers in an array between 1 to 35.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int lotterynumber[7]; 
    // An integer field to save all lottery numbers
    bool equal=false

    for(int i=1; i<7; i++){
        lotterynumber[i]=rand()%35+1; // Randomizes a number between 1 and 35

        for(int j=1; j<i; j++) 
        // Iterating the previously drawn numbers and cross checks it with the current number

            if(lotterynumber[i]=lotterynumber[j])
                equal=true;

        if(equal) 
        // If the current number has previously been drawn it decreases the array by 1 to redo the step until it gets a number not used.

            i--;
            equal=false;


        else 
        // If the current number havent been drawn earlier it gets printed.
            cout << lotterynumber[i] << endl;
    }

    return 0;
}
Sorry, how to delete one of the posts?
Typos:
line 12 needs a semicolon
line 17 needs an open bracket
line 20 needs a == instead of =
line 23 needs an open bracket
line 30 needs an open bracket
Can't believe your compiler allowed any of that.

Logic:

You don't need your bool variable - you can remove it.
You don't need the j loop, you can remove that.
Test to see if your random number != new drawn number instead of equaling it.
If you can use ctime, you can have more random numbers, if not remove it, and the Srand line in main, and you will get the same random numbers every time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// The program draws 7 random "lottery"numbers in an array between 1 to 35.
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{  int j=1;
    srand(time(0));
    int lotterynumber[7];

    for(int i=0; i<7; i++){
        lotterynumber[i]=rand()%35; // Randomizes a number between 1 and 35
        if(lotterynumber[i]!=lotterynumber[j]) {
           cout <<"Ticket #"<<j<<" "<< lotterynumber[i] << endl;
           j++;}
}
return 0;
};


Last edited on
Topic archived. No new replies allowed.