Can't solve this Number Guessing Game problem

I've bought this book 'Beginning C++ through game programming' and it told me how to make a number guessing game, then it challenged me to make a number guessing game where the computer guesses my number. I've been at it for 2 hours, but I can't seem to solve this one problem.
You can say if the number is higher or lower than the number guessed by the computer or spot on. But what I wanted to do is create an exit for when the person just keeps pressing 'higher' or 'lower' so the computer tells them that it is their number because it can't go higher or lower anymore.
I'll post all the code so you can get the whole picture, I'll also place a row of comments above my problem.
This would be the first interactive program I've made, so any tips would be welcome


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
//This here is for the forum, "stdafx.h" is something I have to use because of the compiler i'm using, MS visual studio express 2012
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

int main (){


int gameMode;
int playerGuess, computerGuess, previousGuess;
int higherThan, lowerThan;
int tries;
int secretNumber;
char guessRight;
char playAgain;

cout
<< "Welcome to the number guessing game!\n\n";

gameLoop:

cout
<< "Would you like to:\n"
<< "0. Stop\n"
<< "1. Guess a number\n"
<< "2. Have the computer guess a number\n\n"

<< "I would like to: ";
cin >> gameMode;
cout << endl << endl;

playerGuess = 0;
computerGuess= 0;
tries = 0;
// Resetting the guess and amount of tries in case of replay

srand(static_cast<unsigned int>(time(0)));
// Seeding the number generator for both game modes

if (gameMode == 0)
{
	return 0;
}

else if (gameMode == 1)
{
	secretNumber = (rand() % 100) + 1;

	cout << "The number can range between 1 and 100\n";

	do
	{
		cout << "Guess my number: ";
		cin >> playerGuess;
		++tries;

		if (playerGuess < secretNumber)
		{
			cout << "Nope, higher!";
		}
		
		else if (playerGuess > secretNumber)
		{
			cout << "Nope, lower!";
		}
		
		else
		{
			if (tries == 1)
			{
				cout << "You guessed it in 1 try, well done!!";
			}

			else
			{
				cout << "You guessed it in " << tries << " tries.";
			}
			
			cout << "\n\nWould you like to play again? (y/n): ";
			cin >> playAgain;

			if (playAgain == 'y')
			{
				cout << "\n\n\n";
				goto gameLoop;
			}

			else
			{
				return 0;
			}
		}
		cout << endl << endl;

	} while (playerGuess != secretNumber);
}


else if (gameMode == 2)
{
	computerGuess = (rand() % 100) + 1;
	previousGuess = 0;
	higherThan = 0;
	lowerThan = 101;
	// resetting all computer's guessing values in case of replay
//PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM!!PROBLEM
//My problem is below this text, What i'm trying to do is that when
//the player keeps saying 'h' or 'l' while they said it's higher than say, 74
//but also say it's lower than 74, the program says, "no you're screwing with me, it's 74".
//but I can't get it to work, the most important keywords are 'computerGuess' and 'previousGuess'
//
//
//
//
	do
	{
		cout
		<< "My guess is: " << computerGuess << endl
		<< "is it higher, lower or just right? (h/l/r): ";
		cin >> guessRight;
		cout << endl << endl;

		if (guessRight == 'h')
		{
			previousGuess = computerGuess;
			higherThan = computerGuess;
			computerGuess = rand() % (lowerThan - higherThan) + higherThan +1;
			
			if (computerGuess == previousGuess)
			{
				cout << "you're just fucking with me, it is " << computerGuess;
				guessRight = 'r';
			}
			
			else if (computerGuess == lowerThan)
			{
				--computerGuess;
			}
			
			else {}

		}

		else if (guessRight == 'l')
		{ 
			previousGuess = computerGuess;
			lowerThan = computerGuess;
			computerGuess = rand() % (lowerThan - higherThan) + higherThan +1;
			
			if (computerGuess == previousGuess)
			{
				cout << "You're just fucking with me, it's " << computerGuess;
				guessRight = 'r';
			}
		} 

		else
		{
			if (tries == 1)
			{
				cout << "Ha, that was easy!";
			}
		}

	} while (guessRight != 'r');

	cout << "\n\nWould you like to play again? (y/n): ";
	cin >> playAgain;

		if (playAgain == 'y')
		{
			cout << "\n\n\n";
			goto gameLoop;
		}

		else
		{
			return 0;
		}
}

else
{
	cout << "Please enter a valid option.\n\n\n";
	goto gameLoop;
}
	return 0;
}
Well I'd make the computer guess like this:
(Perhaps you can pick ideas from here?)
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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>

int main()
{
    srand(static_cast<unsigned int>(time(NULL))); //Set the random seed
    int secret_number = 0;

    std::cout << "You will make the computer guess your number!" << std::endl;
    std::cout << "Type in 'r' if computer is right, 'h' if the guess is too low, 'l' if guess if too high.\n" << std::endl;
    std::cout << "Enter the secret number (only b/w you and me): ";
    std::cin >> secret_number;

    int rounds = 1;
    int guess = rand() % secret_number+1;
    std::string you = "z";
    while(true)
    {
        std::cout << "\nYou: The number is somewhere between 0 and " << secret_number+100 << std::endl;
        while(!(you == "r" || you == "l" || you == "h"))
        {
            std::cout << "\nComputer: My guess is " << guess << ". \n" << std::endl;
            std::cout << "Is it right [r/h/l]: ";
            std::cin >> you;
            if(!(you == "r" || you == "l" || you == "h"))
                std::cout << "\nComputer: ME NO UNDERSTAND WHT U SAY!! Y U NO CLEAR!?\n" << std::endl;
        }

        if(you == "r")
        {
            if(guess != secret_number)
                std::cout << "\nComputer: I get the strange feeling that you are lying. BUT it doesn't matter." << std::endl;
            std::cout << "\nComputer: Le me le boss!" << std::endl;
            break;
        }
        else if(you == "h")
        {
            if(guess == secret_number)
            {
                std::cout << "\nComputer: You are a liar!" << std::endl;
                break;
            }
            std::cout << "\nComputer: Damn..OK I'll try again." << std::endl;
            guess += rand() %16;
        }
        else if(you == "l")
        {
            if(guess == secret_number)
            {
                std::cout << "\nComputer: You are a liar!" << std::endl;
                break;
            }
            std::cout << "\nComputer: Damn..OK I'll try again." << std::endl;
            guess -= rand() %16;
        }
        you = "z";
        rounds++;
    }

    std::cout << "\nComputer took " << rounds << " rounds to guess right." << std::endl;

    std::cin.get();
    std::cin.ignore();

    return 0;
}
Here maybe this helps you :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int lower,higher,maxnr,answer,myguess;
    cout<<"Think of a number between 1 and ";cin>>maxnr;
    higher=maxnr+1;lower=1;
    while(true){
    myguess=(higher+lower)/2;
    cout<<"I think it is "<<myguess<<'\n';
    cout<<"Is this the number ?\n";
    cout<<"1.Lower\n";
    cout<<"2.This is it !\n";
    cout<<"3.Higher\n";
    cin>>answer;
    if(answer==1) higher=myguess;
    if(answer==2) {cout<<"It was fun wasn't it\n"; return 0;}
    if(answer==3) lower=myguess;}
    return 0;
}
Heres my other try.Now it detects the number that is left:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void pause()
{
    //Prevents the application from exiting imediatly ...
    cout<<endl<<"Press any key to continue . . .";
    cin.ignore();
    cin.get();
}
int main()
{
    int maxnumber,minnumber=1,answer,number,aux;
    srand(time(0));
    cout<<"Think of a number between 1 and ";cin>>maxnumber;
    ++maxnumber;
    while(answer!=2){
    aux=maxnumber-minnumber;
    if(aux){
    number=rand()%aux+minnumber;}
    else {cout<<"You cheated !\n"; pause(); return 0;}
    cout<<"I think it is "<<number<<endl;
    cout<<"It is this the number ?\n\n";
    cout<<"1.Less\n";
    cout<<"2.This is it!\n";
    cout<<"3.Higher\n\n";
    cin>>answer;cout<<endl;
    if(answer==1) {
    maxnumber=number;
    }
    if(answer==3){
    minnumber=number+1;
    }
    if(maxnumber-1==minnumber) {cout<<"I found your number.It is "<<minnumber<<endl; pause(); return 0;}
    }
    pause();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.