Issues with rand()

Hi all first thank you for taking the time to read this I really appreciate the help.
What my question is im trying to make is a little console game (teaching myself c++) that the computer tries to guess a number you type in. im not sure whats wrong with my code, sometimes its works but the number the computer guesses never changes. Is there something I'm doing wrong or missing??

Many thanks

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
 //Guess My Number



#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{

	int tries
	int Playernumber;
	int toolow = 1;
	int toohigh = 10;

	srand(time(0));
	int Compguess = rand() % toohigh + toolow; // A random number between 1-10
	


	cout << "\tWelcome to Guess My Number\n\n";

	cout << "/tType in your number: ";
	cin >> Playernumber >> //player types in number for computer to guess


	//Now creating the loop the thoughts for this was so that the computer would never
	//choose a number that has already been choosen

	do
	{

		int Compguess = rand() % toohigh + toolow;
		cout << "Computer Guesses: "<< Compguess <<"\n\n";
		++tries
	

		if (Playernumber >  Compguess)
		{
			cout << "Too High!\n\n";
			toohigh = Compguess;
			
		}

		else if (Playernumber < Compguess)
		{
			cout << "Too Low!\n\n";
			toolow = Compguess;
			
		}

		else
		{
			cout << "\nThats it! The Computer wins and it took " << tries <<" guesse(s)!\n"; 
		}
	} while ( Playernumber !=  Compguess);

	return 0;

}
r % x + y = z
implies
y <= z < (x + y)
since
0 <= (r % x) < x
Your code has syntax errors.


It seems that you attempt to narrow down the range lines 45 and 52. That has logical errors.

Lets say that hi=10 and lo=3. Random number is thus in [3..12].
This time, the guess is 7 and that is too low. Logically, you should next look from [8..12].

However, you code sets lo=7 and hi remains 10. The next number will be from [7..16].
You should have adjusted both lo and hi.

What if 7 was too high? You set hi=7, so the new range is [3..9]. It was enough to change the hi, but how to do it wisely?
Last edited on
Hi Synthetics,

I am also a newbie at programming. and its a coincidence that we have a similar idea on a practice program.

I had also wrote a quite similar program in a past days and quite get what you mean.

here's your code that I edited.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
	srand(time(0)); // seed random number
	int tries = 1;
	int Playernumber;
	int Compguess = rand() % 10 + 1; // initialized


	cout << "\tWelcome to Guess My Number\n\n";

	//Now creating the loop the thoughts for this was so that the computer would never
	//choose a number that has already been choosen

	do
	{

		cout << "Type in your number: ";

		cin >> Playernumber; //player types in number for computer to guess
		Compguess = rand() % 10 + 1; // random number 1-10
		cout << "Computer Guesses: " << Compguess << "\n\n";



		if (Playernumber >  Compguess)
		{
			cout << "Too High!\n\n";
			++tries;
			if (tries > 5)
			{
				cout << " computer lose\n";
				cin.get();
				cin.get();
				return 0;
			}

		}

		else if (Playernumber < Compguess)
		{
			cout << "Too Low!\n\n";
			++tries;
			if (tries > 5)
			{
				cout << " computer lose\n";
				cin.get();
				cin.get();
				return 0;

			}
		}
		else
		{
			cout << "\nThats it! The Computer wins and it took " << tries << " guesse(s)!\n";
		}
	} while (Playernumber != Compguess);

	cin.get();
	cin.get();
	return 0;

}


I guess this is what you mean.
I took away 2 variables
and move the Player input in the do-while loop
Last edited on
Thanks everyone for their input it was really helpful. Thank you maiden for the code, it works like a dream!
Topic archived. No new replies allowed.