Number Guessing Game not outputting right

Im trying to create a number guessing game where the user enters number integers they want to guess and the range of those integers 1 to (m). Then the computer randomly generates numbers in that range for the user to guess. The user guesses until they get all the numbers correct For example:

Enter number integers: 4
Enter the range of those integers from 1 to (m): 6
Enter guess: 2 3 1 4
3 of your guesses are correct. Guess Again
Enter guess: 2 3 1 6
Correct. Would you like to play again.

The Output I get right now is:

Enter number integers: 4

Enter the range of those integers from 1 to (m): 6

Enter your guesses for the 4 in the range from 1 to 6 that you have selected: 2 3 1 4


Then after you enter the guesses nothing else happens with the program. I'm confused on why the game is not saying how many guesses where correct.

I don't understand what I'm doing wrong to get this output. I'm also getting an warning where cin >> userNumber in the driver.cpp. It says the no operator matches these operands.

There are three files for this game.cpp, game.h, and driver.cpp. Please let me know if you find anything else wrong with my code. This is my 1st program I have written in C++.

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
// game.cpp
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "Game.h"
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

	//---------------------------------------------------------------------------------------------
	// generateNumbers: Fucntion to generate numbers between 1 to (m) and 
	// generate as many has user wanted.
	// n: the amount of integers
	// m: the max number the user wants
	// numbers: returns the numbers generated by the computer
	//---------------------------------------------------------------------------------------------
	int* Game::generateNumbers(int n, int m) {


		// Declare array size to generate random numbers based on what is between 1 to (m)
		int* numbers = new int[n];

		// Randomly generate n intgers between 1 to (m)
		for (int i = 0; i < n; i++) {
			numbers[i] = (rand() % m) + 1;
			cin >> numbers[i];

		}

		return numbers; // return the amount of n intgers betweeen 1 to (m)


	}


	//-----------------------------------------------------------------------------------------------
	// guessingGame: See's how many numbers the user got to correct until they win the game.
	// n: the amount of integers
	// m: the max number the user wants
	//-----------------------------------------------------------------------------------------------
	void Game::guessingGame(int n, int m) {

		// Declare variables
		int* p;
		int sum = 0;


		// Set inputGuess equal to a vector which is teh size of n
		vector<int> inputGuess;
		inputGuess.push_back(n);

		p = generateNumbers(n, m);

		// Get user guesses integers
		for (int i = 0; i < n; i++) {
			cin >> inputGuess[i];
		}
		// See if the user guesses and computers answers match up
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (p[i] == inputGuess[j]) {
					sum++;
				}
			}
		}

	}

// driver.cpp
#include <iostream>
#include "Game.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
	//Declare variables
	Game guess;
	int n;
	int	m;
	vector<int> userNumbers;


	srand(static_cast<unsigned int>(time(NULL))); // Intialize random number

	// Prompt the user to enter integers and range
	
		// Have user enter number of integers they want
		cout << "Enter the Number of the Integers (n):  ";
		cin >> n;

		// Have user enter the range if integers between 1 to m
		cout << "Enter the Number of Each Integers from 1 to (m):  ";
		cin >> m;

		// guesses from user are not correct then user needs to keep guessing 
		while (userNumbers.size() != n) {
			cout << "Enter your guesses for the " << n << " " << "in the range from 1 to " << m << " " << "that you have been selected: "; 
			cin >> userNumbers;  // Error here says no operator matches these operands
			guess.guessingGame(n, m);
		}

	// If the user has guessed the correct numbers and would like to play again
	if (userNumbers.size() == n) {
		cout << "You are correct! Play Again?";
	}
	// If numbers guessed not correct then display how many are correct
	else {
		cout << sizeof(userNumbers) << "of your guesses are correct." << endl;
	}

};

// game.h
#include <vector>

class Game {
public:
	// Get what numbers the users have guessed are correct
	vector<int> getNumbers() {
		return correct_number;
	}


	// Generate random numbers between the range set by user
	int* generateNumbers(int n, int m);

	// Begin the guessing game
	void guessingGame(int n, int m);

private:
	vector<int> correct_number; // numbers that are correct
	vector<int> input_guess;

};
Last edited on
1
2
3
4
5
6
7
8
9
	// Declare array based on user guesses
	vector<int> inputGuess;

	p = generateNumbers(n,m);

	// Get user guesses integers
	for (int i = 0; i < n; i++) {
		cin >> inputGuess[i];
	}
Your inputGuess vector is empty, but you are trying to put n values into it. If the vector is empty, you should use push_back, or say something like:
inputGuess = vector<int>(n);
You also never clean up the memory allocated by your generateNumbers function.

1
2
3
vector<int> number; // numbers that are correct
	int* randomNumbers; // random numbers generated
	vector<int> inputGuess;
It would seem that one of these variables is redundant. You'd probably have an easier time of you simply used two vectors: vector<int> correct_numbers;, vector<int> input_guesses;

When adding to a vector, you either need to give it an initial size, or use push_back to add to the vector.

1
2
3
4
	// Call the generateNumber function
	generateNumbers(n, m);

	p = generateNumbers(n,m);
You're calling this function twice.

Also note that the local variable called "inputGuess" in your guessingGame function is no the same as your class variable of the same name. It shadows the class variable.
Last edited on
I have tried to follow your advice but now I'm getting it where my project won't even run anymore.
When I try to run I get these errors:
Game.h(10,8): error C2143: syntax error: missing ';' before '<'
Game.h(10,8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Game.h(10,27): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Game.h(22,8): error C2143: syntax error: missing ';' before '<'
Game.h(22,8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Game.h(22,28): error C2238: unexpected token(s) preceding ';'
Game.h(23,8): error C2143: syntax error: missing ';' before '<'
Game.h(23,8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Game.h(23,25): error C2238: unexpected token(s) preceding ';'
Generating Code...
1>Done building project "Driver.vcxproj" -- FAILED.
I have updated this question with my new code.
Please don't edit your OP, it just makes the thread non-linear.

You're trying to speed through this too fast without understanding the fundamentals. Start small, and work your way up.
Last edited on
^^ please post the updated code, and we can fix it.
a lot of times one small error can make it look like you have hundreds of big problems. start at the top, the first one it found, and fix that, recompile, repeat until compiles. A single missing ; can spawn an amazing amount of scary output from the compiler at times.

that said line 106:
you cannot read into a vector like that.
you can do this:
int tmp;
cin >> tmp;
usernumbers.push_back(tmp); //resizes the vector to hold the new data and inserts it at the highest index in the 'array' .. if [0] is the front, and [n] is the 'back'.

or you can do this, slightly better:
usernumbers.resize(n);
cin >> usernumbers[i] ; //assuming loop for i = 0 to n
^^ vectors can be used like arrays. resize here makes sure it has room for n items.
a new vector has room for 0 items. you can also say
vector<int> v(n); //this one has room for n items now, thanks to (n) constructor.

a vector is an object that is a lot like an array. you can use [] and such on it the same way. And just like an array, v = 3; //no good, should be v[index] = 3;
Last edited on
// driver.cpp
#include <iostream>
#include "Game.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
//Declare variables
Game guess;
int n;
int m;
vector<int> userNumbers;


srand(static_cast<unsigned int>(time(NULL))); // Intialize random number

// Prompt the user to enter integers and range

// Have user enter number of integers they want
cout << "Enter the Number of the Integers (n): ";
cin >> n;

// Have user enter the range if integers between 1 to m
cout << "Enter the Number of Each Integers from 1 to (m): ";
cin >> m;

// guesses from user are not correct then user needs to keep guessing
while (userNumbers.size() != n) {
cout << "Enter your guesses for the " << n << " " << "in the range from 1 to " << m << " " << "that you have been selected: ";
for (int i = 0; i < n; i++) { // Added this like you suggested but still getting same errors when the program builds.
userNumbers.push_back(n);
guess.guessingGame(n,m);
}
for (int i=0; i<n: i++){
} cout >> userNumbers[i];
}

// If the user has guessed the correct numbers and would like to play again
if (userNumbers.size() == n) {
cout << "You are correct! Play Again?";
}
// If numbers guessed not correct then display how many are correct
else {
cout << sizeof(userNumbers) << "of your guesses are correct." << endl;
}

};


// game.h
#include <vector>

class Game {
public:
// Get what numbers the users have guessed are correct
vector<int> getNumbers() {
return correct_number;
}


// Generate random numbers between the range set by user
int* generateNumbers(int n, int m);

// Begin the guessing game
void guessingGame(int n, int m);

private:
vector<int> correct_number; // numbers that are correct
vector<int> input_guess;

};

// game.cpp
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "Game.h"
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

//---------------------------------------------------------------------------------------------
// generateNumbers: Fucntion to generate numbers between 1 to (m) and
// generate as many has user wanted.
// n: the amount of integers
// m: the max number the user wants
// numbers: returns the numbers generated by the computer
//---------------------------------------------------------------------------------------------
int* Game::generateNumbers(int n, int m) {


// Declare array size to generate random numbers based on what is between 1 to (m)
int* numbers = new int[n];

// Randomly generate n intgers between 1 to (m)
for (int i = 0; i < n; i++) {
numbers[i] = (rand() % m) + 1;
cin >> numbers[i];

}

return numbers; // return the amount of n intgers betweeen 1 to (m)


}


//-----------------------------------------------------------------------------------------------
// guessingGame: See's how many numbers the user got to correct until they win the game.
// n: the amount of integers
// m: the max number the user wants
//-----------------------------------------------------------------------------------------------
void Game::guessingGame(int n, int m) {

// Declare variables
int* p;
int sum = 0;


// Set inputGuess equal to a vector which is teh size of n
vector<int> inputGuess;
inputGuess.push_back(n);

p = generateNumbers(n, m);

// Get user guesses integers
for (int i = 0; i < n; i++) {
cin >> inputGuess[i];
}
// See if the user guesses and computers answers match up
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i] == inputGuess[j]) {
sum++;
}
}
}

}

// Sorry did not know that would happen.
// Used what you guys suggested but still getting the same errors.
Last edited on
format it, and make sure you saw my edits above, I added more info to what you did on 106.

vector<int> inputGuess;
inputGuess.push_back(n);

this is not what you think.
first, its probably illegal (its code outside of any function block)
and second its not a size to n, it puts n in the [0] location.
you want
vector<int> inputGuess(n); //but, n is not yet defined? does n have a value here?

I think not, instead try

vector<int> inputGuess;

...
main()
{
cin >> n;
inputGuess.resize(n); //now it has room for n items
...
}

OR BETTER: get rid of the global variable and declare it INSIDE main AFTER you know N so you can just use the
vector<int> inputGuess(n); statement.

push_back appends a value into the 'array'. so if you say push_back(1), then 2, 3,4 you have an array {1,2,3,4} where [0] is 1, [1] is 2, ...

if you know how many things you will add to a vector, use resize. If you do not know but can guess, use reserve() and push-back. If you do not know and cannot guess, you still should take a stab with reserve() and push back. Try to avoid using push_back without a reserve. Push back changes the size of the memory the vector uses, and it will have to stop, make new memory block (new a pointer), copy all the old stuff to the new stuff (slow!) and delete the old block, and it will do it over and over as you keep adding more stuff (it has a tweak to allocate more than it needs each time, but it still is not pretty).
Last edited on
You can't call cin >> userNumbers; when userNumbers is a vector. You have to use a loop for each element.

For example:
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
// Example program
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
    // ...
    
	vector<int> userNumbers;
	int n = 0;

	cout << "Enter the Number of the Integers:  ";
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
	    int number = rand() % 10; // or whatever
	    userNumbers.push_back(number);
	}
	
	cout << "Random numbrers generated:\n";
	for (int i = 0; i < n; i++)
	{
	    cout << userNumbers[i] << '\n';   
	}
}
Last edited on
Updated my code above ^^. Still getting the same errors.
I have updated to the way you did it Ganado still having the same errors.
Last edited on
Hello kat9265,

In addition to what Ganado has said here is something different.


I don't understand what I'm doing wrong

For a start: in the ".cpp" files for the C header files should be using the C++ version like "<ctime>" and "<cstdlib>".

using namespace std; is not a good idea. It will eventually cause you problems in the future. Better to learn how and what to qualify with "std::" now while it is only just a few things.

Now is a good time to learn to give your variables a proper name. "m", "n" and "p" may mean something to you, but they could mean something different to some else. For "n" you could use "numOfInts" or "numsToGuess". For "p" maybe "ptrSomething" or somethingPtr". At least it is clear that it is a pointer to something. And "something" would be replaced with what the variable is pointing to.

In line 19 the space between "::" and the function may not be a problem, but it is not generally written that way.

in the for loop, line 26, you are setting numbers[i] = (rand() % m) +1;. That is understandable, but the next line cin >> numbers[i]; will over wright the random number. That is if the user can figure out what they are waiting for. With out a prompt the "cin" does not do much except make the user wonder.

Line 50 calls the generateNumbers function, which returns a pointer, but line 50 never receives this pointer. But you did get it right on line 55.

Again on line 59 the user is wondering what to do.

Any time you have "cin" to get a value you need a prompt to let the user know what and maybe how to enter what is needed.

Lines 62 to 68 I do not understand what you are doing. Also adding to "sum" has me puzzled.

In line 93 "srand" change "NULL" to "nullptr" to be more up-to-date.

The error on line 108, as Ganado has pointed out is an empty vector. It has no size. Your choices are either give the vector an initial size and use an index to the vector, like you would for an array, or use "push_back" to add to the vector. "push_back would be the better choice, so that you do not oversize the vector like you would an array. Done right that should not happen, but how easy is it to type "m" when you meant "n". As a single letter variable it would be hard to find.

That is what I see until I can load the program and compile and test it.

Andy
The code is now running again. Had to quit sing namespace std. Will take your advice you gave me and will let you know if I have any questions or more problems. Thanks for your help.

The sum counts are how integers the user guesses correctly.
Last edited on
Have generateNumbers() return a vector - not a pointer to allocated memory. Then the calling function doesn't have the responsibility of freeing the returned memory. having memory allocated in one place and having to be freed elsewhere is not a good idea.
I have posted my new code in a new discussion called Number guessing game output help. I'm still having trouble with the user not being able to see how many numbers they got right and would they like to play again. The new discussion has new code on it. I have marked this discussion has solved.
Last edited on
Topic archived. No new replies allowed.