Lottery Program -still looking for someone to review latest post

Pages: 12
Hi Everyone,

I have begun an assignment to create a lottery program. I am not allowed to use arrays, as we have not gone over them in class yet, and we were told to use a while loop for the number generation. We must use functions from main only. So i created a numGen function with parameters of p1,p2,p3,p4,p5,p6, representing the 6 numbers to be picked 1-49

Heres where my question comes in. If i start my while loop and use a counter lets say..


1
2
3
4
5
while (c<=6)
{ 
 p1=rand()%49+1;
 c++;
}


How would i change variable out for the next number using the same while loop? Or if this is not possible what should my approach be?
Last edited on
you can make a pointer pRadomNumber, and allocate memory for six int numbers. in while loop, you will be able to move this pointer by using pRandomNumber++; and you'll have access to six different memory areas.
mtweeman anyway you can slightly touch on this a little more? (only been programming for 6 weeks) I understand passing by reference and such and counter increments but allocating 6 int numbers within 1 variable without an array im lost....
Here's a fragment of code from main function:

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
int * p;
	p=(int*)malloc(6*sizeof(int)); //it could be also p=new int[6];

	if(p==NULL)
		{
			printf("can't allocate a memory\n");
			system("pause");
			exit(1);
		}

	int counter=0;

	while(counter<6)
	{
		*p=rand()%49+1;
		p++;
		counter++;
	}

	p-=6;

	for(int counter=0;counter<6;counter++)
		std::cout<<*p<<std::endl;

	free(p);//then it changes to: delete []p;

	system("pause");
	return 0;


The only one problem is that it always generates the same number. I use rand function first time, so you have to find out somewhere in net how make it to be different every time.
Last edited on
ok, after reading up on this site about pointers i completely understand your code and what its doing. heres the problem. We can only use elements in our programs that weve been taught in class already and we haven't got to those types of pointers. only passing values by reference and by value in functions. So is there even a more simplistic way of doing this even if i have to write out more code?

and in reference to you getting the same number over and over again its due to c++ never being able to generate a real random number. in my program, in the main function i have srand(time(NULL));
We were taught to use this to start a random point such as the time the program was being run as a start point
Last edited on
The problem is that he is supposedly not allowed to use arrays and mtweeman is just dynamically allocating an array of integers which is even more complicated then just creating an array on the main function's stack.

If you can't use arrays then you can't use a while loop. You just have to copy and paste the code 5 times unfortunately. The question is were you told not to use arrays or were you told that you don't have to use arrays? You could learn how to use them very quickly as long as you won't fail the assignment over it. Does the instructor not want you to take some initiative?
So is there even a more simplistic way of doing this even if i have to write out more code?


Yes, call rand 6 times and each time assign it to a different integer variable. You can't loop without an array in this case because there is no way to make 6 different variables contiguous in memory.
If you want rand to generate different numbers each time that the program executes use srand to seed the algorithm. Search the web for srand and you'll find examples.
Hi kempo thanks for checking out the post. Yes as you can read above in my last post to mtweeman we can only use elements we have learned. But it specifically says in her assignment post online to use a while loop for the generation of the 6 numbers.

As for initiative, i would love to kick butt and turn in a over the top program showing that i can research and learn things on my own, but i tried that with my first couple of programs (im just that type of person) and i actually was graded down for using things we havent learned.

you are not specifying the problem enough or my english is too weak (it could be second option). you want to say, that pointer must be included in some class? if your answer is "yes" then your program gets more complicated.
no mtweeman,
what i meant was that we can't use those pointers , because we have not learned them in class yet. We can only use the commands we have learned in class to create our programs.
Kempo here is the cut and paste from assignment

Your program should use srand to make sure that the numbers are truly random. The program should just have calls to functions from main, with the only exception that srand can be located in the main function. You must use a while loop to generate the 6 lottery numbers.
@kempofighter,

i think creating a pointer isn't another name for creating an array. rather, we called it memory allocation (not array in memory allocation).

@majesticmixers,

so i don't see another option that create 6 variables and assign to it rand func.
@majesticmixers,

so you can make it this way?

1
2
3
4
5
while(counter<6)
{
cout<<rand()%49+1;
counter++;
}
@mtweeman

hmm... Sure that would output it to the console, but if i had to pass those numbers to another function to export them to a file as well, then wouldnt i need to have them stored in variables?

I only think this way because if i have the numGen function do the random numbers, when i get to the exportFile function if i do it again the numbers will be different...

if you have while loop in function numGen you can simply export data to the file from this function. do you need to make it in exportFile function?
Ok, i can try that out. I can skip exportFile function and do the exporting within numGen, but I have to output to console and to file, so if i had to write the rand line twice its going to generate 2 different sets of numbers
you can also write

1
2
3
a=rand()%49+1;
cout<<a;
someFunction(a);


it'll show it in console and you can pass variable a in every loop to some function.

after your last post i see the problem, but (anyway) solution is above.
Last edited on
mtweenman you hit the nail on the head. Professor emailed me the same time you posted that last reply and its exactly how she said to do it. Thank you. When i finish ill post my code for you to see, and possibly comment on.
ok, no problem dude.
Pages: 12