Computer Guess My Number

Allright,,
To begin with, I'm just a total beginner.
I've started programming a week ago, and I've stumbled upon the "Guess My Number" program. Here is the code I used for the program:


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 "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    srand(static_cast<unsigned int>(time(0)));  //seed the random number generator

	int secretNumber = rand() % 100 + 1;  // create random number between 1 and 100
	int tries = 0;
	int guess;
    
	cout << "\tWelcome to Guess My Number\n\n";

	do
	{
		cout << "Enter a guess: ";
		cin >> guess;
		++tries;

		if (guess > secretNumber)
		{
			cout << "Too high!\n\n";
		}
		else if (guess < secretNumber)
		{
			cout << "Too low!\n\n";
		}
		else
		{
			cout << "\nThat's it! You got it in " << tries << " guesses!\n";
		}

	} while (guess != secretNumber);

    return 0;
}


This works all fine,, But I wanted to make it a bit more interesting,, and let the computer guess My number. Here is what I did:

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	srand(static_cast<unsigned int>(time(0)));

	int tries = 0;
	int iNum;
	int iCompNum = rand() % 100 + 1;

	cout << "\tWelcome to Guess My Number\n\n";
	cout << "in this version, the computer tries to guess YOUR number\n\n\n"; 

	cout << "Enter a number: ";
	cin >> iNum;

	do
	{
		cout << "\nComputer's guess: ";
		cout << iCompNum;
		++tries;

		if (iNum > iCompNum)
		{
			cout << "\nYou're too low..!\n\n";
			iCompNum;
		}
		else if (iNum < iCompNum)
		{
			cout << "\nToo high..!\n\n";
			iCompNum;
		}
		else if (iNum == iCompNum)
		{
			cout << "\nYou got my number, You're the man!\n\nYou guessed the number in just " << tries << " tries!\n\n";
		}

		iCompNum = rand() % 100 + 1;

	} while (iNum != iCompNum);

	return 0;
}


Again,, I'm just a total beginner and don't even know everything I'm writing down. But I thought this would come close to what I wanted to write.
Here are the problems I get with this code:

1) It only goes for a several amount of tries, which is diffrent everytime. And that sounds like it's good, I mean, if it guessed the number, it should stop. But that's where the problem comes up: It stops, but not on the right number. Just a random one.
2) I know there's nothing of this in the code, but I wanted it to listen to the "Go higher" and "Go lower" lines. How do I do that?

-- Any help is Great help,, Thanks!! --

P.S. I know there's another post about this, But in that post the person writes in a whole diffrent way. And I would like (not selfish ment) to write it down this way. Thanks!
Last edited on
Your test to stop the loop is

while (iNum != iCompNum);

Look at your code; when the correct number is guessed (i.e. when iNum == iCompNum) , the very next thing you do is change the value of iCompNum, so they won't be the same any more, and then your

while (iNum != iCompNum);

is testing that new random value of iCompNum
What are lines 30 & 35 supposed to do? They do nothing. Delete them.

If you want to make the computer guess your number (it's really a fraud game since your computer knows your number but to practice with it let's proceed) you can make it insert a number greater or lesser than its previous guess:

Imagine this.
1
2
3
4
5
6
7
8
9
int oldGuess; //this will hold computer's previous guess
...
		if (iNum > iCompNum)
		{
			cout << "\nYou're too low..!\n\n";
			oldGuess = iCompNum;
//this will create a number between oldGuess and 100
			iCompNum = = rand() % (100-oldGuess) + oldGuess; 
		}


You must modify the other case also (iNum < iCompNum). And of course remove line 42 as it's not needed anymore
Last edited on
You should make it so the computer can always guess your number in under 7 tries without entering your number first.
As Moschops said, you don't want to recalculate iCompNum at the end of your loop, this will change the number when you exit. Instead, delete line 42 and calculate it at the start of the loop.

As for lines 30 and 35, this is where you want to redefine the limits of your random number. So don't calculate iCompNum, just calculate the limits. A good way to create limits for a random number is by doing:
MyRandomNumber = rand() % (UpperLim - LowerLim) + LowerLim.

If we stick these suggestions into your code we get:

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

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

	int tries = 0;
	int iNum;
	int UpperLim = 100;
	int LowerLim = 1;
	int iCompNum;

	cout << "\tWelcome to Guess My Number\n\n";
	cout << "in this version, the computer tries to guess YOUR number\n\n\n"; 

	cout << "Enter a number: ";
	cin >> iNum;

	do
	{
		iCompNum = rand() % (UpperLim - LowerLim) + LowerLim;

		cout << "\nComputer's guess: ";
		cout << iCompNum;
		++tries;

		if (iNum > iCompNum)
		{
			cout << "\nYou're too low..!\n\n";
			LowerLim = iCompNum+1;
		}
		else if (iNum < iCompNum)
		{
			cout << "\nToo high..!\n\n";
			UpperLim = iCompNum-1;
		}
		else if (iNum == iCompNum)
		{
			cout << "\nYou got my number, You're the man!\n\nYou guessed the number in just " << tries << " tries!\n\n";
		}

	} while (iNum != iCompNum);

	return 0;
}
Thanks guys!
I've noticed that I'd put unnecessary lines into my code, thanks for letting me know. (:
And after I've followed all the comments, I got the right thing, But when the computer gets to the number in front of my number, the program crashes.
It's like this:

My number: 71 (Example, could be anything between 1 and 100)
Computer number:
- 12 (Too Low)
- 93 (Too High)
- 65 (Too Low)
- 68 (Too Low)
- 70 (Too Low) <-- And here the program crashes.. Why doesn't the computer choose 71? Right now I got this as code:

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	srand(time(NULL));

	int tries = 0;
	int iNum;
	int UpperLim = 100;
	int LowerLim = 1;
	int iCompNum;

	cout << "\tWelcome to Guess My Number\n\n";
	cout << "in this version, the computer tries to guess YOUR number\n\n\n"; 

	cout << "Enter a number: ";
	cin >> iNum;

	do
	{
		iCompNum = rand() % (UpperLim - LowerLim) + LowerLim;

		cout << "\nComputer's guess: ";
		cout << iCompNum;
		++tries;

		if (iNum > iCompNum)
		{
			cout << "\nYou're too low..!\n\n";
			LowerLim = iCompNum+1;
		}
		else if (iNum < iCompNum)
		{
			cout << "\nToo high..!\n\n";
			UpperLim = iCompNum-1;
		}
		else if (iNum == iCompNum)
		{
			cout << "\nYou got my number, You're the man!\n\nYou guessed the number in just " << tries << " tries!\n\n";
		}

	} while (iNum != iCompNum);

	return 0;
}


-Thanks!
Last edited on
It is possible in this code for this to happen:

I pick 2.

Computer guess is 3, so it set the upper limit to 3.

Next guess is 1, so lower limit is 1 + 1 = 2.

Now the computer tries to pick an integer between 2 and 3. *crash*

I think this method is not great anyway. Why not have the computer guess in the middle each time, like this?

I pick 2.

Computer guess is 74 - Too high.
Computer guess is 36 - Too high.
Computer guess is 18 - Too high.
Computer guess is 9 - Too high.
Computer guess is 4 - Too high.
Computer guess is 2 - Yay.

For example.
Yeah I could definitely try that..
After I've tried that I'll just continue learning C++,,
Thanks to all you guys for you Help and Support!!
And a Happy 2012!! ;DD
Topic archived. No new replies allowed.