Passing random numbers to an array

Hey guys and gals,
I'm new to c++ and I have a project that I'm trying to complete but I can't figure out how to pass the random numbers to the array. I think I'm nearly there but there is an error in my code somewhere that I don't understand. I think if I had a little help I could figure the rest of it out. Any suggestions?

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#include <iostream>
#include <ctime>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

const int array_size = 10;

void assign(int array_wins[]);
void draw(int array_wins[], int);
int  entry(int&);
bool check(int, int array_wins[]);
void printout(int, int array_wins[]);

int main() {

	srand(time(nullptr));

	cout << "This program picks 10 random numbers from 1 to 50." << endl;
	cout << "You have a chance to win if you pick the matching number." << endl;
	cout << "Good luck!" << endl;
	cout << endl;	
	
	int array_wins[array_size];

	assign(array_wins);
	
	draw(array_wins, array_size);
	
	cout << "Please select a number from 1 to 50: ";
	int user_input;
	entry(user_input);
	
	check(user_input, array_wins);
	
	printout(user_input, array_wins);
	
	cout << endl;
	cout << endl;
	return 0;
}

// fills array with 0
void assign(int array_wins []) {
	array_wins[array_size] = { 0 };
}

// draws 10 random numbers in array and outputs results
void draw(int array_wins[], int the_number_of_selected_elements) {
		the_number_of_selected_elements = 0;
		
		while (the_number_of_selected_elements < array_size) {
			
			array_wins[the_number_of_selected_elements] = rand() % 50 + 1;
			
			if (check(the_number_of_selected_elements, array_wins))
				++the_number_of_selected_elements;
			else
				return;
		}
}

// gets user's input from 1 to 50
int entry(int& user_entry) {
	
	cin >> user_entry;
	
	cout << " You selected the number: ";
	return user_entry;
	cout << endl; 

}

// checks generated numbers with user number
bool check(int number, int array_wins[]) {
	for (int i = 0; i < number; ++i)

		if (number == array_wins[i]) {
			return true;
		}
		else if (number != array_wins[i])
			return false;
}

// outputs selected lottery numbers
void printout(int user_input_check, int array_wins[]) {
	cout << "You picked the number: " << user_input_check << endl;
	cout << endl;
	for (int i = 0; i < array_size; ++i) 
		cout << "Lotto Numbers: " << array_wins[i] << endl;
	
}
Last edited on
describe the problem? What do you think it should do, and what does it actually do?
I just read the forum rules and I should have described the problem instead of just saying "give me a hint". The program is supposed to take 10 random numbers between 1 and 50 and check them against a user input integer. When debugging, I believe only the first random number is passed to the array and the rest are being given garbage integers. I'm also pretty sure that none of my random numbers are being tested during the check function because the logic is just wrong.
Last edited on
Forum rules aside, I just didn't want to hunt a bug without knowing a symptom :)

your logic says:

loop
assign a value into array
check all values in the array so far
(the array will contain 1 valid entry and 9 garbage values at this time)

if your check function is checking the first value and stopping, it is fine, but redundant. You may want to just check AFTER the values are all assigned once, instead of checking the whole thing every time.

make sure check only checks the values that are valid 'so far' each time, or re-write it so you check at the end once all are assigned.

after you check the value, if it is false, meaning the player did not guess it, the loop ends because it triggers the else/return statement. If that is desired, OK. It seems like the odds of guessing a value are very low and that the program is going to get 1 value, check it, fail, and exit (draw) most of the time, and then do whatever happens after draw (but having only drawn 1 value!)


Last edited on
Alright thanks, I think I fixed the problem with the array but now I have a run-time error (corruption around the stack). I think what is happening is that my draw function is assigning too many values for an array of size 10 which makes me think it is something to do with the while function inside the void draw. Other than that I should be able to figure out the rest.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#include <iostream>
#include <ctime>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

const int array_size = 10;
const int one = 1;

void assign(int array_wins[]);
void draw(int array_wins[], int);
int  entry(int user_input[]);
bool check(int, int array_wins[]);
void printout(int, int array_wins[]);

int main() {

	srand(time(nullptr));

	cout << "This program picks 10 random numbers from 1 to 50." << endl;
	cout << "You have a chance to win if you pick the matching number." << endl;
	cout << "Good luck!" << endl;
	cout << endl;	
	
	int array_wins[array_size];

	assign(array_wins);
	
	draw(array_wins, array_size);

		cout << "Please select a number from 1 to 50: ";
		
		int user_input[one];
		entry(user_input);

		check(user_input[one], array_wins);
	
		printout(user_input[one], array_wins);
	
	cout << endl;
	cout << endl;
	return 0;
}

// fills array with 0
void assign(int array_wins []) {
	array_wins[array_size] = { 0 };
}

// draws 10 random numbers in array and outputs results
void draw(int array_wins[], int the_number_of_selected_elements) {
		the_number_of_selected_elements = 0;
		
		while (the_number_of_selected_elements < array_size) {
			array_wins[the_number_of_selected_elements] = rand() % 50 + 1;

			check(the_number_of_selected_elements, array_wins);
			++the_number_of_selected_elements;
		}
}

// gets user's input from 1 to 50
int entry(int user_entry[]) {
	
	cin >> user_entry[one];
	
	cout << "You selected the number: " << user_entry[one];
	cout << endl;

	return user_entry[1];

}

// checks generated numbers with user number
bool check(int number, int array_wins[]) {

		for (int i = 0; i < number; ++i)
				if (number == array_wins[i]) {	
						return true;
				}
				else if (number != array_wins[i]) {
					return false;	
				}
}

// outputs selected lottery numbers
void printout(int user_input_check, int array_wins[]) {
	cout << endl;
	for (int i = 0; i < array_size; ++i) 
		cout << "Lotto Numbers: " << array_wins[i] << endl;
	
}
The highest valid index for an array is size-1.
What's the point of int user_input[one]; ?
Why don't you just use a single int ?
I think I was trying to compare type int* to int* or something but I see your point. I will see if it changes anything if I change it back.
Hints:
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
71
72
73
74
75
76
77
78
#include <cstdlib>
#include <ctime>
#include <iostream>

constexpr int ARRAY_SIZE = 10;

void fillArrayWithRN(int array_wins[], size_t size);
bool checkArrayAgainstUserInput(int array_wins[], size_t size, int guess);
int  askForInt();
int askForGuesses(int* lottery, int* tickets, size_t size);
void printout(int array_wins[], int tries[], size_t size);

int main()
{
    std::srand(std::time(nullptr));

    std::cout << "This program picks 10 random numbers from 1 to 50.\n"
                 "You have a chance to win if you pick the matching number.\n"
                 "Good luck!\n\n";
    
    int array_wins[ARRAY_SIZE] = {0}; // all fields initialized to 0
    fillArrayWithRN(array_wins, ARRAY_SIZE);
    // Record 10 user's numbers and check them against array_wins
    int usersnum[ARRAY_SIZE] {};
    int matches = askForGuesses(array_wins, usersnum, ARRAY_SIZE);
    std::cout << "You guessed " << matches << " numbers.\n";
    printout(array_wins, usersnum, ARRAY_SIZE);
    std::cout << '\n';
    return 0;
}

// draws 10 random numbers in array and outputs results
void fillArrayWithRN(int array_wins[], size_t size)
{
    for(size_t i{}; i< size; ++i) {
        array_wins[i] = rand() % 50 + 1;
    }
}

int askForGuesses(int* lottery, int* tickets, size_t size)
{
    int matches {};
    for(size_t i{}; i<size; ++i) {
        // Step 1: ask user for input:
        tickets[i] = askForInt();
        if(checkArrayAgainstUserInput(lottery, size, tickets[i])) { matches++; }
    }
    return matches;
}

// checks generated numbers with user number
bool checkArrayAgainstUserInput(int array_wins[], size_t size, int guess)
{
    for (size_t i = 0; i < size; ++i) {
        if (guess == array_wins[i]) { return true; }
    }
    return false;
}

// gets user's input from 1 to 50
int askForInt()
{
    std::cout << "Please select a number from 1 to 50: ";
    int ans {};
    std::cin >> ans;
    std::cin.ignore();
    return ans;
}

// outputs selected lottery numbers
void printout(int array_wins[], int tries[], size_t size)
{
    std::cout << "Lotto numbers are: ";
    for (size_t i = 0; i < size; ++i) { std::cout << array_wins[i] << ' '; }
    std::cout << "\nWhile tried numbers are: ";
    for (size_t i = 0; i < size; ++i) { std::cout << tries[i] << ' '; }
    std::cout << '\n';
}


Output:
This program picks 10 random numbers from 1 to 50.
You have a chance to win if you pick the matching number.
Good luck!

Please select a number from 1 to 50: 3
Please select a number from 1 to 50: 4
Please select a number from 1 to 50: 8
Please select a number from 1 to 50: 19
Please select a number from 1 to 50: 6
Please select a number from 1 to 50: 44
Please select a number from 1 to 50: 7
Please select a number from 1 to 50: 36
Please select a number from 1 to 50: 24
Please select a number from 1 to 50: 11
You guessed 3 numbers.
Lotto numbers are: 37 48 6 27 22 9 19 47 34 44
While tried numbers are: 3 4 8 19 6 44 7 36 24 11

I actually figured it out late (really late) last night into this morning. From what I can see from yours is that I made it way more complex and less (way, way less) terse than it needed to be. It works though so I guess that's good? Here it is:

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

#include <iostream>
#include <ctime>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

const int array_size = 10;

void assign(int array_wins[]);
void draw(int array_wins[]);
int  entry(int& user_input);
bool check(int&, int array_wins[]);
void printout(int, int array_wins[]);

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

	cout << "This program picks 10 random numbers from 1 to 50." << endl;
	cout << "You have a chance to win if you pick the matching number." << endl;
	cout << "Good luck!" << endl;
	cout << endl;	
	
	int array_wins[array_size];
	
	assign(array_wins);
	
	draw(array_wins);

	cout << "Please select a number from 1 to 50: ";	
	int user_input = 0;
	entry(user_input);

	check(user_input, array_wins);
	
	printout(user_input, array_wins);
	
	int i = 0;

// winner is calculated here because in the check loop
// it will always say winner unless i added less terse
// code
	while (i < array_size) {
		if (user_input == array_wins[i])
		{
			cout << "Winner!\n";
		}
		else {
			cout << "";
		}
		++i;
	}
	return 0;
}

// fills array with 0
void assign(int array_wins []) {
	for (int i = 0; i < array_size; ++i) 
		array_wins[i] = { 0 };
}

// draws 10 random numbers in array and outputs results
void draw(int array_wins[]) {
		int the_number_of_selected_elements = 0;
		int array_loop = 0;

		while (array_loop < array_size) {
			the_number_of_selected_elements = rand() % 50 + 1;

			if (check(the_number_of_selected_elements, array_wins) == false) {
				array_wins[array_loop] = the_number_of_selected_elements;
			}
			else {
				--array_loop;
			}
				++array_loop;
		}
}

// gets user's input from 1 to 50
int entry(int& user_entry) {
	cin >> user_entry;
	
	cout << endl;
	return user_entry;
}

// checks generated numbers with user number
bool check(int& number, int array_wins[]) {

// couldn't figure out another way to check both true
// and false against every element that would be more 
// terse so I just went with this.
	{int i = -1;
	do {
		++i;
		if (number == array_wins[i]) {
			return true;
		}
	} while (i < array_size);
	}

// checks for false over every array element
	{int i = -1;
	do {
		if (number != array_wins[i]) {
			return false;
		}
	} while (i < array_size); 
	}
}

// outputs selected lottery numbers
void printout(int user_input_check, int array_wins[]) {
	cout << endl;
	cout << "You selected: " << user_input_check << endl;
	cout << endl;

	cout << "The lotto numbers are:\n";
	for (int i = 0; i < array_size; ++i) {
		cout << "-----> ";
		cout << array_wins[i];
		cout << endl;
	}
	cout << endl;
}


Thanks for everyone's help!
Talemache~
Last edited on
Topic archived. No new replies allowed.