Computer Guessing game

Nov 26, 2012 at 7:12pm
So I'm supposed to make a program where you think of a number and the computer tries to guess it. However, it seems to be getting stuck in an infinite loop and I can't figure out why. Here's my code, any tips?


#include<iostream>
using namespace std;
int main()
{
int NumGuesses,min,max,my_guess,response;
NumGuesses=1;
my_guess=50;

cout<< "Think of a number between 1 and 100. I will attempt to guess your number.\n";
cout<< "Press 1 if the my guess is too high, 2 if it's too low, and 3 if it's right.\n";

cout<< "Is your number " <<my_guess<< " ?\n";
cin>> response;

do
{
max = my_guess - 1;
min = 2;
my_guess = ((max - min)/2 ) + min;
NumGuesses ++;
cout<< "Is your number " <<my_guess<< " ?\n";
}
while(response==1);

do
{
max=99;
min= my_guess+1;
my_guess = (max + min)/2;
NumGuesses ++;
cout<< "Is your number " <<my_guess<< " ?\n";
}
while(response==2);

do
{
cout<< "I got your number in " <<NumGuesses<< " amount of guesses.\n";
}
while(response==3);

system("pause");
return 0;
}
Nov 26, 2012 at 7:44pm
I rewrote your code to flow a little bit better. Also, I didn't do ALL the work, but enough to hopefully let you fill in the blanks and complete your assignment ;) As long as your user eventually enters 3, this will keep you from an infinite loop scenario.

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
#include <iostream>
using namespace std;

int main()
{
  int NumGuesses, min, max, my_guess, response;
  
  cout<< "Think of a number between 1 and 100. I will attempt to guess your number.\n";
  cout<< "Press 1 if the my guess is too high, 2 if it's too low, and 3 if it's right.\n";

  my_guess=50;
  NumGuesses = 0;
  do {
    cout << "\nIs your number " << my_guess << "?";
    ++NumGuesses;
    cin >> response;
    if (cin.fail()) return 1;

    if (response == 1)
    //I guessed too high
    {
     //DECREASE MY GUESS
    } 
    else if (response == 2)
    //I guessed too low
    {
     //INCREASE MY GUESS
    }
    else if (response == 3)
    //I got it right!
    { break; }
    else
    //Entered something other than 1,2, or 3
    { 
    cout << "\nSorry, I didn't understand you";
   //This guess didn't count cause we are "guessing"
   //the same thing when the loop reiterates.
    --NumGuesses;
    }
  } while (response != 3);

  cout << "I got your number in " << NumGuesses << "!\n";

  system("pause");
  return 0;
}
Last edited on Nov 27, 2012 at 12:33am
Nov 27, 2012 at 1:40am
I'm a little confused, aren't you supposed to guess the computer's number?
Nov 27, 2012 at 1:41am
IF not, try using a random number generator
Nov 27, 2012 at 2:51am
From xX420YOLOSwaGGxX's quote on top:

"So I'm supposed to make a program where you think of a number and the computer tries to guess it."
Nov 27, 2012 at 4:10am
Sorry, missed that statement.
Nov 27, 2012 at 6:38pm
Thank you Hellfire, with your help I was able to figure it out.
Nov 29, 2012 at 5:36am
Oh the irony of life... so I was doing a few of the beginner tutorials on this website just to test my skills and I found basically the same problem you did here. So I did the whole program. Since you're complete, I'd love to see your solution to do a little compare and contrast.

Here's mine...

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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int compGuess = 50;
	int newGuess = 0;
	int lastGuess = 100;
		cout << "Think of a number between 1 and 100.  I will try to guess you number.\n";
		cout << "Type 1 if I'm too high, 2 if I'm too low, 3 if I guessed correctly\n\n";
		system("pause");

		int playerResponse = 0;
		do {
			int change;
			cout << "Is your number " << compGuess << "?";
			cin >> playerResponse;

			change = (abs(lastGuess-compGuess)/2);
			if (change == 0) change = 1;

			switch (playerResponse)
			{
				case 1: newGuess = compGuess - change; break;
				case 2: newGuess = compGuess + change; break;
				case 3: cout << "YEAH, I GOT IT!\n"; break;
				default:
					cout << "Sorry, I didn't understand your response!?\n";
					newGuess = compGuess;
			}
		lastGuess = compGuess;
		compGuess = newGuess;
		} while (playerResponse != 3);

	system("pause");
	return 0;
}
Last edited on Nov 29, 2012 at 5:44am
Nov 29, 2012 at 7:03am
why are (at least it seems like it) the two most popular programs hello world!, and you or the program think up a number and the other guesses it?
Nov 29, 2012 at 8:14pm
"Hello World" at this point is more traditional. Every language I've ever learned or attempted to learn started with this. I think it's more used to just verify the compiler and IDE are setup and working correctly. In some cases, might also introduce the future programmer to key or generic libraries (like iostream).

The others are more common for beginners because they are not very complicated, yet touch on the basics of syntax and logic.
Topic archived. No new replies allowed.