Number guessing game output help

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

After I enter the guesses the program does nothing. It should tell me how many of the guesses were correct. What am I missing?

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
144
145
146
147
148
149
150
151
152
153
// game.cpp
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "Game.h"
#include <iostream>
#include <iomanip>
#include <vector>



	//---------------------------------------------------------------------------------------------
	// 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
	//---------------------------------------------------------------------------------------------
std:: vector<int> Game::generateNumbers(int n, int m) {


		// Declare array size to generate random numbers based on what is between 1 to (m)
		std::vector<int> random_numbers;
		int numbers;

		// Randomly generate n intgers between 1 to (m)
		for (int i = 0; i < n; i++) {
		 numbers = (rand() % m) + 1;
		 random_numbers.push_back(numbers);

		}

		return random_numbers;


	}


	//-----------------------------------------------------------------------------------------------
	// 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
		std::vector<int> pointer;
		int correct_answers = 0;
		int user;


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

		pointer = generateNumbers(n, m);

		// Get user guesses integers
		for (int i = 0; i < n; i++) {
			std::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 (pointer[i] == inputGuess[j]) {
					correct_answers++;  //counts how many numbers the user got correct
				}
			}
		}

	}



// game.h
#include <vector>

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


	// Generate random numbers between the range set by user
	std:: vector<int> generateNumbers(int n, int m);

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

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


};

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


int main()
{
	//Declare variables
	Game guess;
	int n; // number of integers
	int	m; // max value for integers
	int guesses;
	std::vector<int> userNumbers;
	


	std::srand(static_cast<unsigned int>(time(nullptr))); // Intialize random number

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

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

		// guesses from user are not correct then user needs to keep guessing 
		while (userNumbers.size() != n) {
			std::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++) {
				userNumbers.push_back(n);
				guess.guessingGame(n,m);
			}

			}
		

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

}; 
Last edited on
Perhaps something like:

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
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

class Game {
public:
	Game(size_t n, int m) { generateNumbers(n, m); }

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

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

	// Begin the guessing game
	void guessingGame();

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


//---------------------------------------------------------------------------------------------
// generateNumbers: Function 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
//---------------------------------------------------------------------------------------------
void Game::generateNumbers(size_t n, int m) {
	generated.resize(n);
	input_guess.resize(n);
	correct_number.clear();

	// Randomly generate n integers between 1 to (m)
	for (auto& g : generated)
		g = rand() % m + 1;

	/*
	// Debug - show the generated numbers
	for (const auto& g : generated)
		std::cout << g << ' ';

	std::cout << '\n';
	*/
}

//-----------------------------------------------------------------------------------------------
// guessingGame: See's how many numbers the user got to correct until they win the game.
//-----------------------------------------------------------------------------------------------
void Game::guessingGame() {
	// Get user guesses integers
	for (auto& i : input_guess)
		std::cin >> i;

	correct_number.clear();

	// See if the user guesses and computers answers match up
	for (size_t i = 0; i < input_guess.size(); ++i)
		if (input_guess[i] == generated[i])
			correct_number.push_back(input_guess[i]);
}

int main()
{
	//Declare variables
	size_t n {}; // number of integers
	int	m {}; // max value for integers

	std::srand(static_cast<unsigned int>(time(nullptr))); // Initialize random number

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

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

	Game guess(n, m);

	// guesses from user are not correct then user needs to keep guessing
	while (guess.getNumbers().size() != n) {
		std::cout << "Enter your guesses for the " << n << " numbers in the range from 1 to " << m << " " << "that have been selected: ";
		guess.guessingGame();
		std::cout << guess.getNumbers().size() << " of your guesses are correct!\n";
	}

	std::cout << "You are correct!\n";
}

Last edited on
seeplus if you do run this code you get an error code LNK2005. Which says "public void_thiscall Game::guessingGame(void) already defined in Driver.obj.
That code is for one .cpp file - no header used or other compilation unit. It doesn't use driver.cpp It compiles and runs OK with MS VS. Just use the code as in my post above - and don't compile with any other .cpp/.h file

For the code I'm working on it must have game.cpp, driver.cpp, and game.h files.
Well you'll need to split my code from above.

L6 - 23 game.h
L26 - 65 game.cpp
L67 - driver.cpp

You'll need to sort out the required #include files
I did what you said. Got it to work right. Thank you so much for your help seeplus :).
Topic archived. No new replies allowed.