help with reset function

Not sure how to define my reset function. My reset function should return NumberGuesser to the state that it was in when it was constructed.

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>
using namespace std;

class NumberGuesser
{
	private:
		int mid;
		int low;
		int high;
	public:
		NumberGuesser(int, int);

		void higher();
		void lower();
		int getCurrentGuess();
		void reset();

};

NumberGuesser::NumberGuesser(int lowerBound = 1, int upperBound = 100)
{
	high = upperBound;
	low = lowerBound;
}

void NumberGuesser::higher()
{
	low = mid +1;
}
void NumberGuesser::lower()
{
	high = mid -1;
}
int NumberGuesser::getCurrentGuess()
{
	mid =(low + high) / 2;
	return mid;
}
void NumberGuesser::reset()
{

}
int main()
{
	NumberGuesser game;
	cout << "Think of a number between 1 and 100\n";
	for (int i=0; i<99; i++)
	{
		char answer;
		cout << "Is the number (h/l/c/r) : " << game.getCurrentGuess() << "?";
		cin>> answer;
		if ('c' == answer)
		{
			cout << "You picked " << game.getCurrentGuess() <<"? Great pick.\n";
		}
		if ('h' == answer)
		{
			game.higher();
		}
		if ('l'== answer)
		{
			game.lower();
		}
		else
		{
			game.reset();
		}
	}
}
You need to put the game in to a while loop instead of your for loop at line 48. The start of the while loop asks them the questions. The end of the while loop asks if they want to try again or exit.
Topic archived. No new replies allowed.