Please help

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.


The problem is that my code only gives me an infinite loop of what looks like the same number. So it's not randomizing at all.
Please Help me, I was due to be done with this 6 days ago, but haven't gotten any time because of shitstorms in my life taking too much of my focus.

Now back to the issue, WHAT HAVE I DONE WRONG?
Been awake for too long worrying about shit but I must to send it in today before I go to sleep at the latest, my teacher contacted me and said so



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;
}

This shouldn't even compile, because your "else" at line 30 isn't correctly matched to an "if".
Last edited on
op don't double post.
It doesn't compile MikeBoy, you are right about that, had written wrong in the post I noticed now, sry been

Last edited on
Why don't you try fixing the code so that it compiles, then you can run it and see how it behaves. Then you can see any problems in the way your new version behaves, try fixing them, and come to us for help if you can't fix them.
Last edited on
I forgot to write else (!(equal)) in my post sry, imagine my code with that written instead of only else


when compiling it begins its infinite loop like i said before

Please help
Can you please post the actual code you are using. Even if you replace line 30 with else (!(equal)), it can't possibly compile.

else (!(equal)) isn't even legal C++.
Problem is I am not home and not on my windows computer,
I am on a borrowed mac right now.
The code I have posted is an earlier version of the one that is working, but no access to my computer for a couple of hours so can't send the fixed one. I only retrieved this one from my dropbox backup.


I remember that I had used a form of NOT Equal statement in conjunction with the else statement and it worked. But I am only a novice in c++ programming so <i am having a hard time recreating my code.

Just so you now, the version that compile still doesn't work correctly


The thing is, if we can't see the code, we can't fix the problems. The logical control statements in the code you posted can't possibly be the same as the ones in your actual code, because they're not legal, so if there's any errors in those in your actual code, we can't know what they are.

One thing I would say is make sure you're using braces to correctly contain your conditional code blocks. Also, you will help yourself a lot if you use a consistent, sensible indentation style, because that will make it easier for you to see and fix mistakes. When I see something like:

23
24
25
26
27
        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;


It immediately sets off alarm bells, because it looks as though you intended equal=false; to be part of the code that only executes if the condition is true, but it will, of course, execute whatever the result of the condition is.

I'd go so far as to recommend using braces even when your conditional block only has a single code statement, to avoid making this kind of error as you edit your code. So, for example, you should prefer:

17
18
19
20
21
22
23
24
25
26
        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;
            }
        }


over what you wrote, because then if you go back and add extra lines to those blocks, you won't accidentally screw up the logic.
Last edited on
Topic archived. No new replies allowed.