Guessing Game

Somehow I still haven't been able to get this code to do what I want it to do. Seems like I cant get the loops right. It debugs but I'm trying to make it in such a way that I can end it anytime with an input of -1. Also, if guess > 100 or guess < 1, then it should print "Out of range". Can you guys help me out? I'm dying over here lol

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void) {
srand(time(NULL));

while(true) {

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


while(true) {
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;

tries++;



if(tries >= 1000000) {
break;
}

if(guess > number) {
cout << "Too high! Try again.\n";
} else if(guess < number) {
cout << "Too low! Try again.\n";
} else {
break;
}


}

while(false) {
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;
cin.ignore();

if(guess == -1) {
break;
}
}

if(tries >= 1000000) {
cout << "You ran out of tries!\n\n";
} else {

cout<<"Congratulations!! " << endl;
cout<<"You got the right number in " << tries << " tries!\n";
}

while(true) {

cout << "Would you like to play again (Y/N)? ";
cin >> answer;
cin.ignore();

if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}

if(answer == 'n' || answer == 'N') {
cout << "Thank you for playing!";
break;
} else {
cout << "\n\n\n";
}
}


cout << "\n\nEnter anything to exit. . . ";
cin.ignore();
return 0;
}
Ive told you in your other posts already. Put your code between code tags - http://www.cplusplus.com/articles/jEywvCM9/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void) {
srand(time(NULL));

while(true) {

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


while(true) {
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;

tries++;

if (guess == -1)
cout << "Thanks for playing!" << endl; {
break;
}

if(tries >= 1000000) {
break;
}

if(guess > number) {
cout << "Too high! Try again.\n";
} else if(guess < number) {
cout << "Too low! Try again.\n";
} else {
break;
}


}

while(false) {
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;
cin.ignore();

if(guess == -1) {
break;
}
}

if(tries >= 1000000) {
cout << "You ran out of tries!\n\n";
} else {

cout<<"Congratulations!! " << endl;
cout<<"You got the right number in " << tries << " tries!\n";
}

while(true) {

cout << "Would you like to play again (Y/N)? ";
cin >> answer;
cin.ignore();

if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}

if(answer == 'n' || answer == 'N') {
cout << "Thank you for playing!";
break;
} else {
cout << "\n\n\n";
}
}


cout << "\n\nEnter anything to exit. . . ";
cin.ignore();
return 0;
}

Sorry! Is this what you were talking about?
How is that any different from your first post... Edit your first post, and follow the instructions in that link.

There is a format button when you click edit, there is an icon <>. Mark all of your code then press it. Just like the link tells you - http://www.cplusplus.com/articles/jEywvCM9/
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void) {
srand(time(NULL)); 

while(true) { 

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


while(true) { 
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;

tries++;



if(tries >= 1000000) {
break;
}

if(guess > number) {
cout << "Too high! Try again.\n";
} else if(guess < number) {
cout << "Too low! Try again.\n";
} else {
break;
}


}

while(false) {
cout << "Enter a number between 1 and 100 (" << 1000000 - tries << " tries left): ";
cin >> guess;
cin.ignore();

if(guess == -1) {
break;
}
}

if(tries >= 1000000) {
cout << "You ran out of tries!\n\n";
} else {

cout<<"Congratulations!! " << endl;
cout<<"You got the right number in " << tries << " tries!\n";
}

while(true) {

cout << "Would you like to play again (Y/N)? ";
cin >> answer;
cin.ignore();

if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}

if(answer == 'n' || answer == 'N') {
cout << "Thank you for playing!";
break;
} else {
cout << "\n\n\n";
}
}


cout << "\n\nEnter anything to exit. . . ";
cin.ignore();
return 0;
}
Dont have much time, I did the guessing game a bit quick for you. Take a look at it and compare stuff. The way you've designed the game is not right.

I also clearly said, Edit your post, not post again.

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
srand(time(NULL));

	int number = 5;//rand() % 100 + 1;
	int guess = 0;
	int tries = 0;
	char answer = 'n';
	do
	{
		cout << "Enter a numbet between 1 and 100 (1000000 tries left): ";
		cin >> guess;

		if (guess > number)
			cout << "Too High!" << endl;

		if (guess < number)
			cout << "Too Low!" << endl;

		tries++;

		if (guess == number)
		{
			number = rand() % 100 + 1;
			cout << endl << "You got the right number in " << tries << " tries!\n";
			cout << "Would you like to play again (Y/N)? ";
			cin >> answer;
			tries = 0;
		}

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

	cout << endl << "Thank you for playing!" << endl;
Last edited on
yea dude, what you can do is click the edit icon on the bottom corner of the post and highlight all code text then click the code tags button. Then delete your reposts.
Topic archived. No new replies allowed.