Reverse Guess my number.

I have a full code of the game, I just can't seem to get my algorithm to work, I have looked around the web for an algorithm that could possibly work. I can' find it and I need it ASAP. I need an algorithm that will get the answer in 7 tries, I have gotten close with my current algorithm, I believe 12 is one of the numbers that breaks it, so try 12. Or just show me some manipulations of my code that will work with the 7 tries parameter! Thank You

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
 //User thinks of a number.
//Computer has to guess within 7 tries.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main(){
	string hint;
    srand(time(0));

	int max = 101;
	int min = 0;
	int tries = 0;

	cout << "Think of a number between 1 and 100." << endl << endl;

	do{
		int Number = 30 % (max - (min + 1))+ (min + 1);	// Algorithm
        cout << "The computer's guess is " << Number << endl << endl;
        cout << "Is the guess high, low, or correct?: ";	// get's hint
		cin >> hint;

		if (hint == "high"){ // the user input "high"
			cout << "The guess was too high." << endl << endl;
			max = Number;
			tries++;
		}

		if (hint == "low"){ // the user input "low"
			cout << "The guess was too low." << endl << endl;
			min = Number;
			tries++;
		}
	}

	//computer guessed correctly, end game loop.
	while (hint != "correct");
		cout << "The computer's guess was correct after " << tries << " tries! Woohoo!!!" << endl << endl;
        cout << "Thanks for playing." << endl << endl;

	return(0);
} // end main 
Why do you use srand(time(0)); if you don't use rand() to generate random number?
I was continuing to try others from the internet, and some of those used rand() so i just kept it up there
Also you use do{} whithout while. It should be do{}while(some_bool)
EDIT:
EDIT 2: made some changes
EDIT 3 : more changes, now works with 100
Try this one:
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
#include <iostream>
#include <iostream>
#include <string>
using namespace std;

int main() {
	string hint;
	int max = 100, min = 1, tries = 0, number = 0;
	cout << "Think of a number between 1 and 100." << endl;

	do {
		number = (max + min) / 2;// Algorithm
		cout << endl << tries + 1 << " computer's guess is " << number << endl;
		cout << "Is the guess high, low, or correct?: ";
		cin >> hint;

		if (hint == "high")
			max = number - 1;
		else if (hint == "low")
			min = number + 1;
		++tries;
	} while (tries < 7 && hint != "correct");

	if (tries == 7 && hint != "correct")
		cout << "I lost! Congrats!";
	else
		cout << "\nThe computer's guess was correct after " << tries << " tries! Woohoo!!!" << endl;
	cout << "\n\nThanks for playing. Press Enter...";
	cin.get();
	cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.