Number guessing game

This program's supposed to randomly generate 6 numbers for the user to guess and calculates the prize depending on how many numbers they guessed correctly? The C++ program uses random number generator, i.e. rand(), function(s), and array(s). There is a syntax error or something on the 25th line and I don't know whats the matter.
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  #include <iostream>
#include <string>
using namespace std;

void swap(int xp, int yp)
{
	int temp = xp;
	xp = yp;
	yp = temp;
}

void selectionSort(int *arr, int n)
{
	int i, j, min_idx;

	for (i = 0; i < n - 1; i++)
	{
		// Find the minimum element in unsorted array
		min_idx = i;
		for (j = i + 1; j < n; j++)
			if ((arr + j) < (arr + min_idx))
				min_idx = j;

		// Swap the found minimum element with the first element
		swap(&arr[min_idx], &arr[i]); //ERROR ON THIS LINEEE
	}
}
int generateRandomNumber(int min, int max) {

	return rand() % (max - min + 1) + min;
}
int *generateRandomArray(int min, int max) {
	static int randomNumbers[5];

	for (int i = 0; i < 5; i++) {
		randomNumbers[i] = generateRandomNumber(min, max);
	}

	return randomNumbers;
}
bool isValidUserInput(int num, int start, int end) {
	if (num >= start && num <= end) {
		return true;
	}

	return false;
}
int * getWhiteBallInputFromUser() {
	static int userInput[5];

	int i = 0;
	int input;
	while (i < 5) {
		std::cout << "Enter number " << i + 1 << " (between 1 and 69): ";
		std::cin >> input;
		if (isValidUserInput(input, 1, 69)) {
			userInput[i] = input;
			i++;
		}
	}

	return userInput;
}

int * getWhiteBallInput(string gameMode, int inputStart, int inputEnd) {
	static int *userInput;

	if (gameMode.compare("Y") == 0 || gameMode.compare("y") == 0) {

		userInput = getWhiteBallInputFromUser();

	}
	else {
		userInput = generateRandomArray(inputStart, inputEnd);
	}

	selectionSort(userInput, 5);

	return userInput;
}
int getRedBallInput(string gameMode) {
	if (gameMode.compare("Y") == 0 || gameMode.compare("y") == 0) {
		int i = 0;
		int input;
		while (true) {
			std::cout << "Enter POWERBALL number " << i + 1 << " (between 1 and 26): ";
			std::cin >> input;
			if (isValidUserInput(input, 1, 26)) {
				return input;
			}
		}
	}
	else {
		return generateRandomNumber(1, 26);
	}
}
/* here implement any logic required */
string getResult(int inputNumbers[], int winningNumbers[]) {
	int result = 0;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (inputNumbers[i] == winningNumbers[j]) {
				result++;
				break;
			}
		}
	}
	if (winningNumbers[5] == inputNumbers[5]) {
		result += 5;
	}

	return std::to_string(result) + ".00";
}
int main() {
	// you can change this according to you
	int winningNumberSet[6] = { 2,10,12,15,30,22 };

	string gamePlayChoice;
	cout << "Number Guessing Game\n";
	cout << "--------------------\n\n";
	cout << "1. Select FIVE numbers from 1 to 69 for the white balls.\n";
	cout << "2. Select ONE number from 1 to 26 for red powerball.\n";
	cout << "3. Prize is determined by how of your numbers match the winning number.\n\n";

	while (true) {
		cout << "Do you want to self pick your white ball numbers(Y or N): ";
		getline(cin, gamePlayChoice);
		if (gamePlayChoice.compare("Y") == 0 || gamePlayChoice.compare("N") == 0 || gamePlayChoice.compare("n") == 0 || gamePlayChoice.compare("y") == 0) {
			break;
		}
	}
	int * whiteBallInput = getWhiteBallInput(gamePlayChoice, 1, 69);

	while (true) {
		cout << "Do you want to self pick your red ball numbers(Y or N): ";
		cin >> gamePlayChoice;
		if (gamePlayChoice.compare("Y") == 0 || gamePlayChoice.compare("N") == 0 || gamePlayChoice.compare("y") == 0 || gamePlayChoice.compare("n") == 0) {
			break;
		}
	}

	int userGamePlayNumbers[6];
	for (int i = 0; i < 6; i++) {
		userGamePlayNumbers[i] = *(whiteBallInput + i);
	}
	userGamePlayNumbers[5] = getRedBallInput(gamePlayChoice);

	string result = getResult(userGamePlayNumbers, winningNumberSet);
	cout << "******** Game Report *********\n\n";
	cout << "You won " << result << " points for this Game.\n\n";
	cout << "Here are your numbers: ";
	for (int i = 0; i < 6; i++) {
		cout << userGamePlayNumbers[i] << " ";
	}
	cout << "\n\n";
	cout << "Here are winning numbers: ";
	for (int i = 0; i < 6; i++) {
		cout << winningNumberSet[i] << " ";
	}
	

	system("pause");
	return 0;
}

Last edited on
closed account (E0p9LyTq)
line 25 logically wrong could read swap(arr[min_idx], arr[i]);.

Your line 25 is passing two addresses into the function that require two ints. But you will be swapping two temporary local variables. I doubt you want that.

There are two alternatives that you should consider, both require rewriting your swap function.

Alternative 1 (keeping line 25 the same):
5
6
7
8
9
10
void swap(int* xp, int* yp)
{
   int temp = *xp;
   *xp = *yp;
   *yp = temp;
}

Now you properly swap the two values, using pointers and addresses.

Alternative 2 (rewriting the function and line 25):
5
6
7
8
9
10
void swap(int& xp, int& yp)
{
   int temp = xp;
   xp = yp;
   yp = temp;
}

swap(arr[min_idx], arr[i]);

Properly swap two values using references.
Last edited on
big help, thanks
FurryGuy, your pointer example is swapping local variables as well :)
[It's since been fixed, for any readers]
Last edited on
How would I create a function that takes two parameters: an int array and an int variable for the size of the array that saves the content of the array to a file.
Something like this?
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
// Example program
#include <iostream>
#include <string>
#include <fstream>

// function that takes two parameters:
// an int array and
// an int variable for the size of the array
// that saves the content of the array to a file.
bool save_array(int arr[], int size)
{
    std::ofstream ofs("out.txt");
    if (!ofs)
        return false;
        
    for (int i = 0; i < size; i++)
    {
        if (!(ofs << arr[i] << " "))
            return false;
    }
    return true;
}

int main()
{
    int arr[] = {5, 2, 3, 4, 7};
    const int size = 5;
    if (save_array(arr, size))
    {
        std::cout << "Success!\n";   
    }
    else
    {
        std::cout << "Failure!\n";   
    }
}


Last edited on
yes! ty
closed account (E0p9LyTq)
FurryGuy, your pointer example is swapping local variables as well :)

That comes from rewriting the function correctly and then copying a previous version, yeesh.
Ganado wrote:
FurryGuy, your pointer example is swapping local variables as well :)


This statement confuses me immensely. I thought about it for a good 5 minutes but I still don't know why 'local variables are being swapped' (and it seems FurryGuy did not correct you on that either, he replied giving the impression that there is a possibility that what you're saying could be right in given circumstances??)

I guess the confusion is between passing address to the function, which was FurryGuy's implication and passing pointers itself. Local copies are made of the pointer being passed in case of passing a pointer. But our intention is not to change the address of the pointers. So where is the question of local variables here?

In short, why would FurryGuy's function be "swapping local variables"? Regardless of whether you pass address or pointer, the function will change the value at address.. )and we don't care about the addresses itself).

I'm NOT CRITICIZING. I'm just intrigued on why nobody corrected Ganado. From how I read it, Ganado said that passing using pointers is wrong, which was my cause for confusion.
Hi Nwb, FurryGuy edited his post at 2:02am (my local time) to fix it. You posted at 8:00am (6 hours later). FurryGuy's post is correct now. Hope that clears things up.
Makes sense. I'm curious, but what was it before the edit?
Topic archived. No new replies allowed.