Guessing game c++

Hi i am new here,i am trying to learn c++. I am making a number guessing game, the game check for valid input, if the guess is in range,if the guess allready has been guessed,tells the player if the guess is higher or lower then the randomised number, and gratulate the plyer when the right number has been guessed, there is two other things i am hving troubble with, if the player plays again the number of guesses doesnt reset to zero and the randomised number doesnt change, those two things is what i got stucked on, i have tried to put the srand(time(0)); and int number = rand() % 100 + 1; in the while loop so that runs if the player desides to play again i have written a specified GenNumber function but that did mess upp the code so the structure and logic is important, kind reegards


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
  #include<iostream>
#include<ctime>
#include<array>
#include<string>
#include<cstdlib>
using namespace std;





bool allreadyguessed(int guesses[], int guess)
{

	for (int i = 0; i < 25; i++)
	{
		if (guesses[i] == guess)
			return true;
	}
	return false;
}

bool checkvalidguess()
{
	if (std::cin.fail())
	{
		std::cin.clear();
		std::cin.ignore(1000, '\n');

		return false;

	}
	return true;
}



int main(void) {
	
	int index = 0;
	char answer = 0;
	int guesses[100] = { -1 };
	int count = -1;
	int guess =0;
	char input;
	int mostAttempts = 0;
	int totalguesses = 0;
	string name;
	bool t = true;
	answer = count++;
	cout << "enter your name " << endl;
	cin >> name;
	cout << "welcome to this super awesome action game " << name << endl;

	while (true) {

		int number = rand() % 100 + 1;
		int guess;
		int tries = 0;
		char answer;

	game:
		while (true) {

			std::cout << "Enter a number between 0 and 100 " << endl;
			std::cin >> guess;

			std::cin.ignore();

			

			if (checkvalidguess() == false)
			{
				cout << " enter a number!, not random stuff " << endl;
				continue;
			}


			if (guess < 0 || guess > 100)
			{
				cout << "invalid guess, try a guess in range " << endl;

				tries--;
			}
			else if (allreadyguessed(guesses, guess))
			{
				cout << "allready guessed" << endl;

			}
			
			
			
			else if (guess > number) {
				cout << "you guessed way to high  " << endl;

				guesses[index] = guess;
				index++;

			}
			else if (guess < number) {

				cout << "you guessed to low!try again " << endl;

				guesses[index] = guess;
				index++;

			}


			else {
				break;
			}
			tries++;
		}



		

		if (guess == guess) {

			std::cout << "Congratulations!! " << name << std::endl;
			std::cout << "You guessed the right number in " << tries << " tries ";
		}
		while (true)
		{
			cout << "would you like to play again ?" "press y for yes and n for no " << endl;
			cin >> answer;
			cin.ignore();

			if (answer == 'y' || answer == 'Y') {
				goto game;
			}
			else if (answer == 'n' || answer == 'N') {
				std::cout << "Thank you for playing!";
				std::cout << "Enter anything to exit " << std::endl;
				std::cin.ignore();
				break;
			}
			else
			{
				std::cout << "------------";
			}
		}


	}


	return 0;
}
.
Last edited on
You need to reset your guess variable and reload your random number variable before calling goto game, line 132.

To develop it further, you could make the game a 'class' with a 'constructor' which did this.
Hello maksoud,

You have several other problems with your program than just resetting some variables.

Line 44 defines the variable "guess" and line 58 defines the variable "guess". Normally this would be a compiler error, but because of "scope" line 58 is in the scope of the while loop and never sees or used the "guess" defined on line 44. You should do some reading on "scope". http://www.cplusplus.com/doc/tutorial/namespaces/

Avoid using "goto" statements. If the program is done correctly this will not be needed.

Line 120 Did yo mean if (guess == number)? As is you are checking "guess" against it-self and it will always be true.

This is from a recent post that is very similar to your program that might help.
http://www.cplusplus.com/forum/beginner/248311/#msg1094799 The rest of that post may also be useful and a response from coder777, in a previous message, has a shorter version.

Line 125 The while loop is not necessary just the contents of the loop. Removing the while loop will also eliminate the need for the "goto".

Hope that helps,

Andy
thanks for the replies, yeah i know that there is things to work on when it comes to the structure, first i am just gonna try to make the program do what i want it to do then i will try to work on that. yeah i will check out the post you linked to thanks
i have managed to reset count and managed to get other numbers randomised but and how to clear my array std::fill(guesses, guesses+100, 0); i think it is correct thanks
Last edited on
Registered users can post here. Sign in or register to post.