Need help with project

Hello I am new to this C++ and so far Im enjoying it when Im getting it. I have been struggling with this project for a few days and have used your forums as a guest to get me this far. I'm hoping someone can help me finalize this. Please and thanks
I have 3 issues which I made comments next to
1. random number generator isn't working, giving garbage
2. my qualifier to ensure numbers are under 10 isn't working and has made me add an asterisk, I don't know what that is doing
3.I added a cout in secondary function to see numbers before transfer for T-shooting but that isn't working either. In the end I don't need that

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
  /* James Smith
week5 assignment 1
lottery generator*/
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//function prototype
double randomnum(int);

int main ()
{
//declare variables
int lottery[5], user[5];

//introduce program
cout << "Is today your lucky day?? Play the lottery and find out! \n" << endl;
//get user numbers
cout << "Please input your 5 lucky numbers between 0 and 9! \n";
for (int i=0; i<5; i++)
{
 cout <<"Lucky number"<< (i+1)<<": ";
cin >> user[i];
while(*user>10)// why is this making me add a star and not working
{
	cout <<"The number must be between 0 and 9, please reenter" << endl;
	cin >> user[i];
}
}
//generate lottery numbers
randomnum;

//display lottery numbers
for(int i=0; i<5; i++)
{
cout << lottery[i] <<" " << endl;
 }
//declare if a winner or not
for (int n=0; n<1; n++)
{
	if (user[n] == lottery [n])
	cout << "You were right it was your lucky day!! Congratulation you matched all 5 numbers!!" << endl;
	else
	cout << "I'm sorry you did not match all 5 numbers please play again" << endl;
		
	}
	

return 0;
}

double randomnum(int lottery[])
{
	srand(time(0));
		int num[5];
		for (int i =0; i<5; i++)
		{
			return num[i] =rand()%9;//getting garbage numbers
			
			for(int i=0; i<5; i++)// this is in here to display numbers 
			{                     //before swap, it's not working
			cout << num[i] <<" " << endl;
			 }			
		}

}
On line 33, when you call the function you dont give it any arguments. you need to give it arguments if it has any (yours does) you pass an int array into the function so you need to do that with your prototype as well. So your function prototype needs to be:

double randomnum(int[]); Passing an array of ints so you need the [] next to int.

and for your function call to randomnum, it would look like this:

randomnum(lottery);


ENTIRE CODE:

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
  /* James Smith
week5 assignment 1
lottery generator*/
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//function prototype
double randomnum(int[]);

int main ()
{
    //declare variables
    int lottery[5], user[5];

    //introduce program
    cout << "Is today your lucky day?? Play the lottery and find out! \n" << endl;
    //get user numbers
    cout << "Please input your 5 lucky numbers between 0 and 9! \n";
    for (int i=0; i<5; i++)
    {
        cout <<"Lucky number"<< (i+1)<<": ";
        cin >> user[i];

        while(*user > 10)// why is this making me add a star and not working
        {
            cout <<"The number must be between 0 and 9, please reenter" << endl;
            cin >> user[i];
        }
    }
    //generate lottery numbers

    randomnum(lottery);

    //display lottery numbers
    for(int i=0; i<5; i++)
    {
        cout << lottery[i] <<" " << endl;
    }
    //declare if a winner or not
    for (int n=0; n<1; n++)
    {
        if (user[n] == lottery [n])
        cout << "You were right it was your lucky day!! Congratulation you matched all 5 numbers!!" << endl;
        else
        cout << "I'm sorry you did not match all 5 numbers please play again" << endl;

    }


    return 0;
}

double randomnum(int lottery[])
{
	srand(time(0));
		int num[5];
		for (int i =0; i<5; i++)
		{
			return num[i] =rand()%9;//getting garbage numbers

			for(int i=0; i<5; i++)// this is in here to display numbers
			{                     //before swap, it's not working
			cout << num[i] <<" " << endl;
			 }
		}

}
Last edited on
Thank you, I corrected those two. I did originally have randomnum(lottery); but had removed it trying to correct it. I also added some content to the output of the lottery numbers so the user knows what is being output. I still don't have the random number generator working tho, I'm not sure why
Anyone else able to help me please. I have read numerous forums and i don't know why my random numbers are coming out so odd. I get 0,0,0,0,1375001664 every time. Also my while loop for if a number is over ten only works for the first entry.
Hi,

Can explain these?

Why the randomnum function returns double? What is the difference between double and int - especially how they are stored?

What does the return statement do? How does affect anything that comes after it?

If a function returns a value, what needs to happen where the function was called from?

How does one refer to an individual element in an array? Hint - need a subscript to specify which one.

Gotta run - am at work :+)
I put the function back into the body of the main program.
As I am still learning myself I am not sure how to return an array.

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
  /* James Smith
week5 assignment 1
lottery generator*/
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int main ()
{
    //declare variables
    int lottery[5], user[5];

    //introduce program
    cout << "Is today your lucky day?? Play the lottery and find out! \n" << endl;
    //get user numbers
    cout << "Please input your 5 lucky numbers between 0 and 9! \n";
    for (int i=0; i<5; i++)
    {
        cout <<"Lucky number"<< (i+1)<<": ";
        cin >> user[i];

        while(*user > 10)// why is this making me add a star and not working
        {
            cout <<"The number must be between 0 and 9, please reenter" << endl;
            cin >> user[i];
        }
    }
    //generate lottery numbers

	srand(time(0));

    for (int i =0; i<5; i++)
    {
        lottery[i] = rand() % 9;//getting garbage numbers
    }

    //display lottery numbers
    for(int i=0; i<5; i++)
    {
        cout << lottery[i] <<" ";
    }
    cout << endl;

    //declare if a winner or not
    for (int n=0; n<1; n++)
    {
        if (user[n] == lottery [n])
        cout << "You were right it was your lucky day!! Congratulation you matched all 5 numbers!!" << endl;
        else
        cout << "I'm sorry you did not match all 5 numbers please play again" << endl;

    }

    return 0;
}

@eclipser93

I am fixing your for loop problem where you're getting your numbers, 0 to 9, and you need the asterisk in the while line. The whole loop, should look like this.

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 5; i++)
	{
		cout << "Lucky number " << (i + 1) << ": ";
		cin >> user[i];

		while (user[i] > 9)// Check what's in user[i]
		{
			cout << "The number must be between 0 and 9, please reenter Lucky number " << (i + 1) << ": " << endl; // added extra text.
			cin >> user[i];
		}
	}
Thank you whitenite1 I see where i went wrong with that now.
@coderunner I tried to put it in the body as well with the same results.
I've done tons of reading and it seems like I'm doing the random number correct
Hello, I comes from China, my English is poor, so that I cannot describe the problem really, but I want to tell you what I think, please pay attention,thanks
there are three problems as follows:
1、
1
2
3
4
5
while(*user>10)// why is this making me add a star and not working
{
	cout <<"The number must be between 0 and 9, please reenter" << endl;
	cin >> user[i];
}

I think the right code is
1
2
3
4
5
while(*(user+i)>10)// why is this making me add a star and not working
{
	cout <<"The number must be between 0 and 9, please reenter" << endl;
	cin >> user[i];
}


2、
1
2
3
4
5
//function prototype
double randomnum(int);

//generate lottery numbers
randomnum;


I think is
1
2
3
4
5
//function prototype
double randomnum(int *);

//generate lottery numbers
randomnum(lottery);


the parameter should be a point to int,and the array name is a point too

3、
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double randomnum(int lottery[])
{
	srand(time(0));
		int num[5];
		for (int i =0; i<5; i++)
		{
			return num[i] =rand()%9;//getting garbage numbers
			
			for(int i=0; i<5; i++)// this is in here to display numbers 
			{                     //before swap, it's not working
			cout << num[i] <<" " << endl;
			 }			
		}

}


I think is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void randomnum(int * lottery)
{
	srand(time(0));
		for (int i =0; i<5; i++)
		{
			lottery[i] =rand()%9;//getting garbage numbers
                }
			
	         for(int i=0; i<5; i++)// this is in here to display numbers 
		{                     //before swap, it's not working
		          cout <<lottery[i] <<" " << endl;
	        }			

}


thanks
Topic archived. No new replies allowed.