Comparing Integers to Same Array Twice

I know where the problem is but am still confused. When I run this program the lastCloseness variable stays at 57 every iteration. This is strange because the currentCloseness variable changes properly. What am I doing wrong?

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 #include <iostream>
#include <cmath>

using namespace std;

//  A guessing game where the user trys to find a fixed number between 1-1000.

//  The program asks for a number and tells the user whether they are getting closer or farther
// away from one of the elements of the array using a loop.  Will terminate after success.


int distanceToClosest(int solutions[], int SIZE, int guess);
bool determineIfWinner(int solutions[], int guess);




int main()
{
	// initialize array and variable
	int solutions[5] = { 57, 142, 340, 513, 850 };
	int currentCloseness = 0, lastCloseness = 0, guess = 0, lastGuess =0;
	const int SIZE = 5;

	// explain game and ask for input
	cout << "\t--Number Guessing Game-- \n";
	cout << "Try to guess one of the lucky numbers between 1 and 1000 \n";
	cout << "Please make a guess: ";
	cin >> guess;

	// ensure number is within range
	while (guess < 0 || guess > 1000)
	{
		cout << "Please enter an integer 1-1000: ";
		cin >> guess;
	}

	// determine if number is correct, if so end program
	bool win = determineIfWinner(solutions, guess);
	if (win == true) {
		cout << "Success On Your First Try! Good Job! \n";
		return 0;
	}
	else
	{
		cout << "I am sorry, that is incorrect! \n";
	}

	// start do-while loop that will terminate if number picked
	do
	{

		// ask user to make a guess
		cout << "Please make a guess: ";
		cin >> guess;

		//ensure number is within range
		while (guess < 0 || guess > 1000)
		{
			cout << "Please enter an integer 1-1000: ";
			cin >> guess;
		}

		// determine if input is further or closer than previous input to an array number and
		// display "warmer" or "colder" based off of closer or not
		currentCloseness = distanceToClosest(solutions, SIZE, guess);
		lastCloseness = distanceToClosest(solutions, SIZE, lastGuess);

		if (currentCloseness < lastCloseness)
			cout << "Getting warmer! \n";

		else if (currentCloseness > lastCloseness)
			cout << "Getting colder! \n";

		else if (currentCloseness == lastCloseness)
			cout << "Same distance away! \n";
		else
			cout << "Success! \n";

		
		// change the value of lastGuess before restarting the loop
		guess = lastGuess;
		
		// end do-while loop if number is guessed and end program
	} while (currentCloseness != 0);



	return 0;
}

// find the distance of the current guess to the nearest solution 
int distanceToClosest(int solutions[], int SIZE, int guess)
{
	int closeness = 1000;
	for (int i = 0; i < SIZE; i++)
	{
		if (abs(guess - solutions[i]) < closeness)
			closeness = abs(guess - solutions[i]);
	}
	cout << "Error test closeness " << closeness << '\n';//debugging output

	return closeness;

}

// function to determine if the guess matches any of the solutions
bool determineIfWinner(int solutions[], int guess)
{
	for (int count = 0; count < 5; count++)
	{
		if (guess == solutions[count])
			return true;
	}

	return false;
}
See line 82 and comment on line 81.
Last edited on
Topic archived. No new replies allowed.