Beginner's Exercises

I stumbled upon an old thread presenting and discussing exercises for beginners.
http://www.cplusplus.com/forum/articles/12974/

I have been tackling these one by one and was looking for some help on a few of them. The first one is the bracketing search.
I am new to programming and am teaching myself. So any tips would be helpful, but the main reason why I'm posting is that I'm having a problem with the PRNG.
When the computer gets close to figuring out the user's number it will repeat numbers 3-4 times or will exceed the nLow or nHigh.
A secondary thing I was looking for help on is that the program only outputs the "Shucks! I'll try again." part every two times it actually guesses.
Any help is appreciated.


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

char difference;
int compGuess = 'a';
int guessCount = 0;
int nHigh = 100;
int nLow = 0;
void guessWork();
void number();

int main (){
	cout<< "Please pick a number between 0 and 100," <<
	" but do not tell me what it is." << endl;
	sleep (3);
	cout << "I will begin to guess the number you picked." << endl
	<< "If I guess too high, please tell me by typing 'h,'" 
	<< " but if I guess too low, type 'l.'"<< endl
	<< "If I guess correctly, please answer 'c.'"<< endl;
	sleep (3);
	
	while ( difference != 'c'){
		number();
		cout << "Is your number "<< compGuess << "?"<< endl;
		guessCount++;
		cin>> difference;
		guessWork();
	}
	if (difference == 'c'){
		cout<< "Man! I guessed it already?" <<
		"You're not too good at this. ;)"<< endl
		<< "It took me "<< guessCount 
		<< " time/s to get it right." << endl;
		
	}
}
void guessWork(){
	if (difference == 'h'){
		nHigh = compGuess;
			cout<< "Shucks! I'll try again." << endl;
			
		number();
			cout<< "Is your number "<< compGuess << "?" << endl;
			cin >> difference;
		}	
	if (difference =='l'){
		nLow = compGuess;
		cout << "Shucks! I'll try again."<< endl;
			
		number();
			cout<< "Is your number " << compGuess << "?" << endl;
			cin >> difference;
	}
}

void number(){
	srand(time(0));
	
	compGuess = (rand() %(nHigh - nLow +1) + nLow);

}
closed account (Dy7SLyTq)
i remember reading some code for that a long time ago where the guy did it in 7 tries by splitting it in half each time
You do not want to seed the random number generator every time you get a random number. Generally, you only want to seed the generator once per program run.

If a guess is too high or too low, you want to remove that number from the range of possibilities, which means lines 40 and 48 need an adjustment.

1
2
3
4
5
if ( difference == 'h' )
{
    nHigh = compGuess-1 ;
    // ...
}


You call number which sets the computer guess in the loop in main and in guessWork. When it is called in guessWork your guess boundaries are not updated accordingly. I would just remove lines 43 to 45 and 51 to 53 completely.
Thanks for the help cire. I moved the seed into the main function. I bumped the while loop down a line a stuck it there.

The mistake on lines 40 and 48 made sense. I thought it was kind of obvious after I read your comment. It will still repeat numbers but not as frequently.

I made the adjustment to lines 43 to 45 and 51 to 53 like you said, and it works. What was making it not update accordingly?
Last edited on
I made the adjustment to lines 43 to 45 and 51 to 53 like you said, and it works. What was making it not update accordingly?


Your computer guess is only updated in guessWork when the function is entered. What happens then is that you ask for input on another guess. Then you return to main where you ask for input on yet another guess. Then you enter guessWork where the boundaries are updated when the function is entered. So you skipped updating boundaries every other guess.
Oh man lol.

It's so obvious now that you explained it lol. Thanks for the help. I appreciate it.

I made another guessing game without using PRNGs each guess and I tried the half increments like DTSCode posted. It's a lot more concise and it gets it in at most seven guesses.
Topic archived. No new replies allowed.