Thread: Needs help making a lottery

1. Needs help making a lottery

I have begun to make a lottery and the code likes this so far

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int myNum[7];
int drawNum[7];

for (int selectYN = 1; selectYN < 7; selectYN++)
{
cout << "Enter number " << selectYN << ": ";
cin >> myNum[selectYN];

if (myNum[selectYN] > 50)
{
do
{
cout << "You didn't enter a valid value! " << endl;
cout << "Enter number " << selectYN << ": ";
cin >> myNum[selectYN];
} while (myNum[selectYN] > 50);
}
}
cout << "Your line is: ";
for (int enteredNums = 1; enteredNums < 7; enteredNums++)
{
cout << myNum[enteredNums] << " ";
}
return 0;
}```
The problem is that drawNum7 I want to call random numbers from it like:

But that's not possible to do like that because this is an array and needs two {, } after =. I thought about that I could compare myNum7 with drawNum7 with a for loop then.

Is it any way that I can say that all drawNum7 is random draw between 50 and 1?.

2. It is a bit unclear from your question what your problem is but i think you would like to have a random set of numbers drawn for your lottery?

A simple way to get a random number is like this:

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(time(NULL));

int rNum = 0;

rNum = rand() % 50 + 1;

std::cout << rNum << "\n";

}```

3. Yes that's exactly what I mean. But you can not do it with an array. Like this: int rNum[7] = rand() % 50 + 1. That's the problem. I could have done something like rNum1, rNum2, rNum3. But I want a easier way if possible.

4. Originally Posted by DecoratorFawn82
Yes that's exactly what I mean. But you can not do it with an array. Like this: int rNum[7] = rand() % 50 + 1. That's the problem. I could have done something like rNum1, rNum2, rNum3. But I want a easier way if possible.
you can do
Code:
``` int rNum[7];
rNum[0] = rand() % 50 + 1.
rNum[1] = rand() % 50 + 1.
....```
Leaves just the problem of duplicates,
but you could put that into a function that checks if the number was already drawn.
Kurt

5. Does that work?. That didn't I know but thanks!. It should be possible to use a for loop to compare my numbers with the random numbers huh?. I guess so.

6. you can just use a function containing a loop to assign your aray the random numbers, supressing duplicates - a while loop is good for this. On the other hand as this is C++ you can use std::set

7. I suppose that this works well too.

Code:
```    int drawNum[7];
srand(time(0));
for (int i = 1; i < 7; i++)
{
drawNum[i] = rand() % 50 + 1;
cout << drawNum[i] <<  " " << endl;
}```

8. yes but you could have the same number selected several times - you need work to prevent that happening

9. What's wrong with this code I need something to compare my numbers with random numbers.

Code:
```            for (int cmpNum1 = 1; myNum[cmpNum1] < 7; cmpNum1++)
{
for (int cmpNum2 = 1; drawNum[cmpNum2] < 7; cmpNum2++)
{
if (myNum[cmpNum1] == drawNum[cmpNum2])
{
cout << "Yes";
}
}
}```

10. Originally Posted by DecoratorFawn82
What's wrong with this code ....
You are not checking the first elements and your loop condition is wrong . should check for cmpNum1 and cmpNum2 < 7.
Kurt

11. Oh, you mean that I check 1 < 7 instead of 0 < 6 huh?

I have already tried with 0 < 6 too. But that won't work for me too.

I changed the code and tried again but if you have entered 19 as the 6th number.
And then has it at 3rd number at the random number line. Should the program say that you have one number correct then?

12. that code is hideous, use a constant to define the extent of each loop

13. A const int?. To give all loops a value of 6?. I don't understand how that make differens. If I not misunderstood you.

14. Originally Posted by DecoratorFawn82
A const int?. To give all loops a value of 6?. I don't understand how that make differens.
By using a named constant instead of a magic number, you would make your code more readable and hence easier to understand. Furthermore, in the event that the number is to change, changing a constant is safer (and probably faster) than trying to change every relevant instance of a magic number.

15. Originally Posted by laserlight
By using a named constant instead of a magic number, you would make your code more readable and hence easier to understand. Furthermore, in the event that the number is to change, changing a constant is safer (and probably faster) than trying to change every relevant instance of a magic number.
I would refer to
SourceForge.net: Safer arrays in Cpp - cpwiki

Anyway, the problem (with your code, which is not very good code) is that it should be something like
for (int cmpNum1 = 0; cmpNum1 < 7; cmpNum1++)
Indexes starts at 0, and you want to check every index.

But I urge you to read the article above for some insight into arrays and problems.