Computer Guesses Number in Game - Not working, why? No debug warnings..

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

using namespace std;
using std::string;

int main()


	
{
	srand(static_cast<unsigned int>(time(0)));

	int origRnum = rand() % 100 + 1;

	int playAgain;
	int guess = 0;

	int tries = 0;
	int playerNumber = 0;
	int hiLow = 0;

	beginning:

	cout << "Welcome to Guess My Number!\n\n";
	cout << "Select a number for the computer to guess from 1 to 100: ";
	cin >> playerNumber;
	cout << "\n\n";

	

	if (playerNumber > 100)
		{
			cout << "\n\nInvalid selection, please choose a number from 1 to a 100: ";
			cin >> playerNumber;
			cout << " \n\n";
		}
	else if(playerNumber <= 100)
		{
		
			cout << "Thank you, the computer will now guess your number!";

		}


	cout << "\n\nThe computer's guess is " << origRnum << ".\n";
			++tries;
		
			cout << "\nIs the guess higher or lower than your number(1-higher, 2-lower)?\n";
			cout << "1 - Higher, and 2 - for lower (1 or 2): ";
			cin >> hiLow;
			cout << "\n\n";

			


	while (guess != playerNumber)
	{
		++tries;
		
		
		if (hiLow == 1)
		{
			
				int guess = (rand() % (playerNumber - origRnum)) + 1;
				cout << "The computer's guess is " << guess << ", is this higher or lower than your number, 1-higher, 2-lower: ";
				cin >> hiLow;
		
		}

		else if (hiLow == 2)
		{
			
			int guess = (rand() % (origRnum - playerNumber)) + 1;
			cout << "The computer's guess is " << guess << ", is this higher or lower than your numer, 1-higher, 2-lower: ";
			cin >> hiLow;
			
		}
	}


	cout << "The computer guessed " << playerNumber << " in " << tries << ", want to play again? (y/n): ";
	cin >> playAgain;
	
	if (playAgain == 'y')
	{
		goto beginning;
	}

	else if (playAgain == 'n')
	{
		cout << "Goodbye, thanks for playing!";
		
	}


So this is what I've come with for an assignment out of a book. Wants you to create a game where the computer gueses the users number. I could have cheated and looked at others but this is what I got on my own. To me, it looks like it should work. except it has only guessed the correct number maybe once and then it didn't seem to exit the "while" loop when it did so I'm not sure what's wrong with it.
You have two variables named guess. The while loop can only see the first one, however, you're creating another one within the scope of while that gets terminated every time the while loop, loops. To alleviate this, remove int from the second declarations of guess, these reside within the for loop.
Okay, so I removed the 'int" from the 'guess' variables. However, the game still isn't playing out. The computer doesn't guess very well lol. Is something wrong with the random number between two positive number generator?

guess = (rand() % (origRnum - playerNumber)) + 1

After some research this looked like what I needed. In the example above I have this run if the player replies to the program that the number guessed by the computer is higher than their selected number. It is the opposite if they select that it is lower...

Well, that's not a very good algorithm. Consider this: numbers 1-100 are valid, if the previous guess was too high, you want to take the last number, subtract the range that is still valid, and generate a random number. If it was too low, generate a number in the valid range, and add it to guess. You'll guess within range. If you want a code example, I can provide one for you.
Yes please, I'd like the example. I'm already way beyond what the book has provided to accomplish this program. I'm sure I'm making it way more complicated then it had to be. What did you have in mind?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int hival = 100, int lowval = 1;

// ...

guess = origRnum;

while (guess != playerNumber)
	{
		++tries;
		
		cout << "The computer's guess is " << guess << ", is this higher or lower than your number? (1 - higher | 2 - lower): ";
		cin >> hiLow

		if (hiLow == 1) // Needs to go lower
			hival = guess;

		else // Needs to go lower
			lowval = guess;
		guess = rand() % (hival - lowval) + 1;
	}


I haven't tested it, but in theory that should work. It also eliminates the need for your first set of hiLow checks before the while loop. See if you can implement that and see what the results are.
I think I got it. Let me know if I'm interpreting the code correctly.

So the variable 'guess' seeds the 'hival' and 'lowval' variables the numbers to be inputted after the 'if' statement correct?

Rather than how I had it where each time the player would select either 1 or 2 for the 'hiLow' variable which would seed the random number algorithm in separate statements.


One question, are the comments "Needs to go lower" both supposed to say the same thing?

Also, what's the deal with the "guess = origRnum" at the top? And after an "if' statement, you don't need to put '{' and '}' in them?
Last edited on
One question, are the comments "Needs to go lower" both supposed to say the same thing?


No, the second one is supposed to say Needs to go higher

Also, what's the deal with the "guess = origRnum" at the top?


The purpose behind that is so that I could rearrange your cout statements and also allow you to remove the cout statements and hiLow checks before the while statement. If the computer guesses right on the first try, you're screwed because you have to respond if it was higher or lower, but it's the same.

This also allows you to simplify your code down to only what's needed, nothing more.

And after an "if' statement, you don't need to put '{' and '}' in them?


No, if an if/else is missing it's brackets, it will read the next statement only. I'll teacher you something, observe:
1
2
3
4
5
6
if (something)
   Do something;
else if (something else)
   Do something else;
else
   Do everything else;


Actually translates to this:
1
2
3
4
5
6
7
if (something)
   Do something;
else
   if (something else)
      Do something else;
   else
      Do everything else;


Now with brackets:
1
2
3
4
5
6
7
8
9
if (something) {
   Do something;
}
else if (something else) {
   Do something else;
}
else {
   Do everything else;
}


Same thing as:
1
2
3
4
5
6
7
8
9
10
11
if (something) {
   Do something;
}
else {
   if (something else) {
      Do something else;
   }
   else {
      Do everything else;
   }
}


Something to take notice to in both of the "same thing as" is the fact that there is no else if's. This is because the term else if doesn't exist in C++. It's really a twist on the unlimited zero space allowed by the language, and the use of the bracketless if/else's. To put it simply, C++ requires all else's to have exactly one if, and every if can't have more than one else. else if's don't exist. This is proof that you have been using bracketless if/else statements without even knowing it ;)

So the variable 'guess' seeds the 'hival' and 'lowval' variables the numbers to be inputted after the 'if' statement correct?


Yes and no. The purpose of hival and lowval is to limit the options the computer can guess. Do yourself a favor and grab a pen and a piece of paper and follow your code as it does it. You know what guess is equal to because you display it each time. You also can tell what hi/lowval is going to be because of the if statements, and you know what your number is since you choose it. See if it makes sense during a run of your code.

Sometimes if you're unsure how exactly things work, paper and pencil/pen work wonders. I still use that technique today to debug my code when I'm confused on how it should behave. Take this code I just finished:
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
template<class T>
void vp::list<T>::sort() {
   if (size() <= 1)
      return;

   for (int merges = 0, k = 1; merges != 1; k *= 2) {
      node *p = firstNode, *q = p;

      for (int qsize = merges = 0; p; merges ++, p = q) {
         for (qsize = 0; qsize < k && q; qsize ++, q = q->next);

         while (qsize && q) {
            if (q->value <= p->value) {
               if (q->next) {
                  q = q->next;
                  q->prev->insert_before(p);
               }
               else {
                  q->insert_before(p);
                  q = NULL;
                  lastNode = lastNode->prev;
               }

               if (p == firstNode)
                  firstNode = firstNode->prev;

               qsize --;
            }
            else
               p = p->next;
         }
      }
   }
}
Topic archived. No new replies allowed.