Computer guess my number

I can't figure out this code in chapter 2 beginning c++. I thought that it shouldn't be as different as the guess my number is they just switch rolls, but I can't get it.I read others post with the same game but still I don't understand.
The code I did gives me an infinite loop with is "Nooo too high"
I think that I have to update the compuess, but I don't know.
Please somebody help. Thank you very much in advance!

This is the code:

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

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int number;
int compguess = (rand() % 100) + 1; // random number between 1 and 100 ;
int tries = 0;

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

cout << "What number do I have to guess between 1 and 100?: ";
cin >> number;

do
{

cout <<"\n Is this number: "<< compguess <<"\n";
++tries;
if (compguess > number)
{
cout << " Nooo too high!\n\n";

}
else if (compguess < number)
{
cout << "Nooo too low!\n\n";

}
else
{
cout << "\nThat's it! You got it " << compguess << " \n";
}

} while (compguess != number);

return 0;

Yes you need to update the compguess - at the moment the computer makes one 'guess' at the start of the code but then doesn't update its guess from that point onwards.

How about adding some code to your too high / too low statements to make another guess
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 <cstdlib>
#include <ctime>

using namespace std;

int main()
{ 
int number;
int compguess = (rand() % 100) + 1; // random number between 1 and 100 ;
int tries = 0;

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

cout << "What number do I have to guess between 1 and 100?: ";
cin >> number;

do
{
    srand(static_cast<unsigned int>(time(0)));
    int compguess = (rand() % 100) + 1;


cout <<"\n Is this the number: "<< compguess <<"\n";
++tries;
cin.get();
if (compguess > number)
{
cout << " Nooo too high!\n\n";
cin.get();

}
else if (compguess < number)
{
cout << "Nooo too low!\n\n";
cin.get();

}
else
{
cout << "\nThat's it! You got it " << compguess << " \n";
cin.get();
}

} while (compguess != number);

return 0;
}
good! Thank you!! compguess is updated! but still I have the problem with the loop, it keep saying "Nooo too high, it never goes to too low so, I am going to try by adding the statements with high and low..let see what happens
Oh, I see! I'll work on this once I return home!
Thank you!, I can't get it..I am stuck! because at one moment the comp got the number, but it didn't understood the number guesses before was too high or too low..i keep working on it
Try this! This should work!

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

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

using namespace std;


int rand_1toN100(int x);

int main()
{
srand(time(NULL)); //seed random number generator
int number;
int compguess;


int tries = 0;

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

cout << "What number do I have to guess between 1 and 100?: ";
cin >> number;





     
                 



for(int i = 0; i <= 5; i++){
        compguess = rand_1toN100(100) + 1;
        cout <<"\n Is this number: "<< compguess << endl;
        tries++;
        
        
        if(compguess == number){
                    cout << "yeah that is right! " << endl;
                    break;
                    }
                    
                    
   if(compguess < number)
                         cout << "Too low! " << endl;
                         
                         
                         
  if(compguess > number)
                         cout << "Too high! " << endl;
                         i++;
                         
  
       
      
      
      
      
      
      }
      
      
      
           
                         
          
                         
                    
        
        

 
       
   
      
           
      
      





             
                  
            system("PAUSE");
             return 0;
             }
             
             int rand_1toN100(int x){
                 
                 return rand() % x;
                 }                 
    
    
                           


I am heavy sorry for spacing xD. So mutch new lines. This code is much smaller than yours :P.
Last edited on
You know some code that you are using like rand_1toN100(int x) or NULL Idon't know yet.....
I add the cin.get() and it updates, but now the problem is that the computer keep guessing with knowing that the number guesses before was to high or too low...
I think that I need to update that part...'
Okay, so what you want is for the program to guess the number and once it's guessed a number and it's too high to not guess a number that's higher than that and vice versa?
exactly!!!....and what I'm going try now it to use break; once the number is guessed.

thank you!
Now, with break; at least the program stops when the number is guessed!
Oh man, I can't figure this one out haha
me either!!ahah, oh my god and that is chapter 2 I don't wanna imagine the next ones!
You needed a lowest and highest variable to store the lowest and highest guess. Then you need to tell the rand function to only guess numbers within this range.

After that, you're all set! ^^

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

using namespace std;

int main()
{
	int number;
	int high = 100;		// lowest number to guess
	int low = 1;		// highest number to guess
	int compguess;
	int tries = 0;
	
	cout << "\t~ Welcome to Guess Your Number ~\n\n";
	
	cout << "What number do I have to guess between "
	<< low << " and " << high << ": ";

	cin >> number;
	cin.get();
	
	do
	{
		srand(time(0));
		compguess = (rand() % (high - low - 1)) + low + 1;

		/* The line below is an algorithm based solely on
		 * mathmatics. To use it simply uncomment its line.*/
		
		//compguess = low + ((high - low) / 2); 
		
		cout <<"\n Is this the number: "<< compguess << "?\n";
		++tries;

		if (compguess > number)
		{
			cout << "Nooo too high!\n\n";
			high = compguess;
		}
		else if (compguess < number)
		{
			cout << "Nooo too low!\n\n";
			low = compguess;
		}

		cin.get();

	} while (compguess != number);

	cout << "\nThat's it! It took you "<< tries << " tries! \n";
	
	return 0;
}
Last edited on
Awesome! Thanks for posting that carebearboy, it's been bothering me that I haven't been able to figure it out.
Thank you so much, carebearboy!..I was all day yesterday trying to figure out! and actually with the way you did it you don't need break; to stop the loop!

Thank you again!
I'm sorry to interrupt again but I had to make an edit. The program as it was would not guess 1 or 100 (or the highest or lowest number in any case) which leads to a flawed guessing system. I edited lines 26, 39, and 44 to correct this. It now works perfectly.

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

using namespace std;

int main()
{
	int number;
	int high = 100;
	int low = 1;
	int compguess;
	int tries = 0;

	cout << "\t~ Welcome to Guess Your Number ~\n\n";

	cout << "What number do I have to guess between "
	<< low << " and " << high << ": ";

	cin >> number;
	cin.get();

	do
	{
		srand(time(0));
		compguess = (rand() % ((high - low) + 1)) + low;

		/* The line below is an algorithm based solely on
		 * mathmatics. To use it simply uncomment its line.*/

		//compguess = low + ((high - low)/ 2);

		cout <<"\n Is this the number: "<< compguess << "?\n";
		++tries;

		if (compguess > number)
		{
			cout << "Nooo too high!\n\n";
			high = compguess - 1;
		}
		else if (compguess < number)
		{
			cout << "Nooo too low!\n\n";
			low = compguess + 1;
		}
		cin.get();

	} while (compguess != number);

	cout << "\nThat's it! It took you "<< tries << " tries! \n";

	return 0;
}
Last edited on
Topic archived. No new replies allowed.