Game wont loop to resart

I built a high low game that includes functions and levels. I have it almost completed but I can get it to restart. Thank you for your help.


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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//Includers
#include <iostream>
#include <iomanip> 
#include <ctime>
using namespace std;

//Function to print out the welcome banner
void printwelcome()
{
	//Display Chart
	cout << "******************************************" << endl;
	cout << "****   Welcome to the High-Low game!  ****" << endl;
	cout << "******************************************" << endl;

	// Display difficulty
	cout << "Choose the difficulty level: " << endl;
	cout << setw(16) << "1. Easy" << endl;
	cout << setw(18) << "2. Medium" << endl;
	cout << setw(16) << "3. Hard" << endl;

}
//This is the main function that contian the levels and user inputs
int main()
{

	srand(time(NULL));                //the function that generates random numbers
	int number = rand() % 100 + 1;    //the range of the random numbers
	int lvl;
	int guess;
	int triesx = 0;					//The guess is stored here
	int tries = 0;  //The number of tries stored here
	char answer = 'n';
	char digit;//The answer to the question is stored here
	digit = 'm';
	
	do {
		printwelcome();

		//
		cout << "Number Generated: " << number << endl;
		//Makes Sure to loop if someone doesnt enter a valid level number
		while (true)
		{
			cout << "Level: ";
			cin >> lvl;
			if (lvl == 1 || lvl == 2 || lvl == 3)
			{
				break;
			}
			else
			{
				cout << "This is not a level" << endl;
				cout << "Please choose a number 1-3 for difficulty" << endl;
			}
		}
		//This is level Easy to user get 20 tries 	
		if (lvl == 1)
		{
			tries = 20;
			cout << "You choose Easy!" << endl;
			while (tries <= 20 && tries >= 1
				&& digit == 'm' || digit == 'M')
			{

				cout << "You have " << tries << " Tries" << endl;
				cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
				cout << "Guess here: ";
				cin >> guess;    //The guess is stored here
				tries = tries - 1;
				triesx++;

				if (guess == 0 || guess > 100)                              //If statement that produces an error message if user enters a number out of the peramiters
				{
					cout << "This is not an option try again/n" << endl;    //Error message
				}

				if (number == guess)  //If the user guesses the number
				{
					cout << "Tries left: " << tries << endl;
					tries++;

					tries = tries - 20;

					cout << "Congratualtions!! " << endl;  //Message printed out if the user guesses correctly
					cout << "You got the right number in " << triesx << " tries" << endl;  //Lets the user know how many guess they used

				}
				if (number < guess)//if the guess is to high
				{
					cout << "Too high" << endl;  //This message prints if guess it to high
				}
				if (number > guess)  //if the guess is to low
				{
					cout << "Too low" << endl;  //This message prints if the guess is to low
				}

				if (tries >= 20)   //If the user uses all their guesses
				{
					cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
				}


			}
		}
		//This is level Medium the user get 15 tries	
		if (lvl == 2)
		{
			tries = 15;
			cout << "You choose Easy!" << endl;
			while (tries <= 15 && tries >= 1 && digit == 'm' || answer == 'M')
			{

				cout << "You have " << tries << " Tries" << endl;
				cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
				cout << "Guess here: ";
				cin >> guess;    //The guess is stored here
				tries = tries - 1;
				triesx++;

				if (guess == 0 || guess > 100)                              //If statement that produces an error message if user enters a number out of the peramiters
				{
					cout << "This is not an option try again/n" << endl;    //Error message
				}

				if (number == guess)  //If the user guesses the number
				{
					cout << "Tries left: " << tries << endl;
					tries++;

					tries = tries - 15;

					cout << "Congratualtions!! " << endl;  //Message printed out if the user guesses correctly
					cout << "You got the right number in " << triesx << " tries" << endl;  //Lets the user know how many guess they used

				}
				if (number < guess)//if the guess is to high
				{
					cout << "Too high" << endl;  //This message prints if guess it to high
				}
				if (number > guess)  //if the guess is to low
				{
					cout << "Too low" << endl;  //This message prints if the guess is to low
				}

				if (tries >= 15)   //If the user uses all their guesses
				{
					cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
				}

			}
		}
		//This is level Hard the user get 10 tries
		if (lvl == 3)
		{
			tries = 10;
			cout << "You choose Easy!" << endl;
			while (tries <= 10 && tries >= 1 && digit == 'm' || digit == 'M')
			{

				cout << "You have " << tries << " Tries" << endl;
				cout << "Enter a number between 1 and 100: " << endl; //The user is asked for a guess
				cout << "Guess here: ";
				cin >> guess;    //The guess is stored here
				tries = tries - 1;
				triesx++;

				if (guess == 0 || guess > 100)                              //If statement that produces an error message if user enters a number out of the peramiters
				{
					cout << "This is not an option try again/n" << endl;    //Error message
				}

				if (number == guess)  //If the user guesses the number
				{
					cout << "Tries left: " << tries << endl;
					tries++;

					tries = tries - 10;

					cout << "Congratualtions!! " << endl;  //Message printed out if the user guesses correctly
					cout << "You got the right number in " << triesx << " tries" << endl;  //Lets the user know how many guess they used
					
				}
				if (number < guess)//if the guess is to high
				{
					cout << "Too high" << endl;  //This message prints if guess it to high
				}
				if (number > guess)  //if the guess is to low
				{
					cout << "Too low" << endl;  //This message prints if the guess is to low
				}

				if (tries >= 10)   //If the user uses all their guesses
				{
					cout << "You've run out of tries!" << endl; //The message that prints when a user is out of guesses
				}
			}
		}
		
		
		cout << "Do you want to restart Y/N ?";
		cin.ignore(256, '\n');
		cin.get();

	} while (answer == 'n' || answer == 'N');

	//System pause here	
	system("pause");
	return 0;
}/*

 */
You didn't assign anything to variable 'answer'. Its value is always 'n', as it was set at the start.
oh ok ill try that thank you for your response! :)
okay so I took char answer = 'n'; out and made it char answer;
then it gave me an error
so I went down to the cin.get();
changed it to answer = cin.get()
but it still doesn't loop the program if I choose y for yes. it just closes it
while (answer == 'y' || answer == 'Y');
Take a look at line 204, } while (answer == 'n' || answer == 'N');
It will continue to loop, because char answer = 'n'; . What you may want to do is change the while statement to } while (answer != 'n' && answer != 'N');


1
2
3
4
5
6
		cout << "Do you want to restart Y/N ?";
		cin >> answer;
		//cin.ignore(256, '\n');
		//cin.get();

	} while (answer != 'n' && answer != 'N');


If you want to generate a random number every restart, then you may want to place the int number = rand() % 100 + 1; inside the do/while loop. Furthermore, if you want to reset the number of tries the user takes to answer the question on every restart, then you should put it inside the do/while loop, as well, like so:

1
2
3
4
	do {
		printwelcome();
		int triesx = 0;					//The guess is stored here
		int number = rand() % 100 + 1;    //the range of the random numbers 


Take a look at the lines 60, 109, 156. They all indicate Easy. I hope it helps.

Last edited on
It works !Thank you Kevin yours worked! Chicofeo thank you I tried yours and it worked but kevin got to you before you did. Thank you so much i've been sitting here for a 2 hours trying to get it to work!
The beauty of it is there are different ways to have the same result. Isn't it great? Good luck!
closed account (E0p9LyTq)
Another 4 variants to consider (make sure you include <cctype>):

while (toupper(answer) == 'Y'); or while (tolower(answer) == 'y'); or
while (toupper(answer) != 'N'); or while (tolower(answer) != 'n');
Topic archived. No new replies allowed.