Vector value comparing

I am writing a game where the player has to guess a random number between 1 - 100. It now tells if the inserted number is higher or lower than the correct random number, but I would like to add a new feature.

Would it be possible to somehow compare the players guesses to the correct answer and instead of saying "The number is lower than X" I would like it to say "The number is lower than X. The correct answer is between X and Y."

This means that I would need to compare the values added into the vector lastGuess to the variable correct and print the closest lower and higher value in place of X and Y.

I imagine that a problem would also be that there is no higher or lower value after the very first guess.

Could someone give me a hint here? If this is too advanced for a newbie like me please tell it also. I don't know where to start.

There is no any real reason for implementing this feature, I just want to learn. :)

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
int main()
{

	srand(time(NULL));
	int correct = (rand()%100)+1;
	int guess;
	int guesses = 0;
	int maxGuesses = 8;
	int i = 0;
	
	size_t size = 8;
	vector<int> lastGuess(size);
	
	cout << "\nGuess the number." << endl;
	cout << "It must be between 1 - 100. You have only 8 attempts." << endl << endl;
		
	do
	{
			cout << "Insert your guess: ";
			cin >> guess;
			guesses++;	//Total number of guesses			
			lastGuess[i++]=guess; //Guesses in a list
	
			if(guess < correct)
			{
					cout << "The number is higher than " << guess << ". " << endl;
					cout << "That was your " << guesses << ". guess. " << endl << endl;
			}
			else if(guess > correct)
			{
					cout << "The number is lower than " << guess << ". " << endl;
					cout << "That was your " << guesses << ". guess. " << endl << endl;
			}
			else
			{
					break;	//After correct answer or max number of attempts passed the loop ends.
			}
	} while (guess != correct && guesses<maxGuesses); 
	
	if(guess==correct)
	{
			
			system("cls");
			cout << "\nCorrect! The answer was " << correct << ". " << endl;
			cout << "You needed to guess " << guesses << " times to get it right." << endl;
			cout << "\nYour guesses were: ";
	
			i=0;
	
			for(guesses; guesses>0; guesses--)
			{
					cout << "[" << lastGuess[i++] << "] ";
			}
			
			cout << "\n";
			cin.ignore();
	}
	else
	{
				system("cls");
				cout << "You lost. You weren't able to get it right with " << maxGuesses << " attempts." << endl;
				cout << "The correct answer would have been " << correct << "." << endl;
				cin.ignore();
	}
		
}
The way I would approach this is on the first guess I would state that the number is either higher or lower than the guess given. Then with the second guess, I would state it would be either be between or still higher/lower than the guess.

To make it know if something is either higher, lower, or in between the guesses, I would use subtraction with an int to determine how to output the hints. So, if the I guessed is lower than the number being guessed, I would do something like my_guess - correct_answer = -n.
the '-' in this case would state that the guess was lower so I would say that the right number is greater than the guess. Do the same with a higher number except the correct_number would be positive.

Now to determine when someone is getting closer, I would do this through the array of guesses the user has done and keep the numbers that were closest to 0 (since when the guess minus the answer would result in 0). For example, as I was to guess from a number that was originally lower than the answer and kept guessing lower, the negative number would get closer to 0. The same would happen if I was going from the highest number guessed and kept working my way down to the answer.

So overall, just keep track the lowest and highest numbers that are close to 0 based on a '-' sign or a '+' sign. That's how I personally would address this.
Topic archived. No new replies allowed.