Help with Number guessing Program (Possible Loop Issue)

Good afternoon,

I am tasked with writing a program that is a number guessing program between 1-1000. The numbers are always the same and in a sorted array. When the user guesses, it should tell the user if the number is right, wrong or out of range. And then the second guess should tell the user if they are closer or further away using warmer/colder.

My problem (I believe) is in my loop. When I run the program, I can enter any of the numbers in the array the first time and its correct. I can enter a number out of range and get an error. If I enter a different number than a correct one, I get that it is incorrect and then every other number I enter after that it just either says it is warmer or colder. Even if I put a number that is in the array into the program it still says that I am either warmer or colder. I really have no idea what I am doing wrong and any help I can get will be greatly appreciated even though I know some people do not really like helping out with homework.

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
#include <iostream>
#include <cmath>

using namespace std;

//Write the Function prototype
int distanceToClosest (const int solutions[], int SIZE, int guess);

int main()
{
	//Be sure to initialize the array and the variables.
	const int SIZE = 5;
	const int solutions[SIZE] = { 31, 111, 351, 752, 999 };
	int distance = 0, secondDistance = 0, guess = 0, nextGuess = 0;

	//Describe the game to the user and ask for their input
	cout << "\t\t---The Guessing Game---" << endl;
	cout << "Welcome to the Number Guessing Game!" << endl;
	cout << "Can you guess the correct number? Give it a try!" << endl;
	cin >> guess;

	//Be sure the number is in the correct range (1-1000)
	while (guess < 0 || guess > 1000)
	{
		cout << "Error! Please pick a number between 1-1000!" << endl;
		cin >> guess;
	}

	//Figure out if user has guessed the correct number. If so, close program.
	for (int i = 0; i < SIZE; i++)
	{
		if (guess == solutions[i])
		{
			cout << "You were correct on your first try! Congratulations!" << endl;
		return 0;
	}
	else if (guess != solutions [i])
	{
		cout << "Sorry! that is incorrect!" << endl;
	}
	//Start of the do-while loop that will close if the number is picked.
	do
	{
		//Have the user make another guess
		cout << "Please pick a number!" << endl;
		cin >> nextGuess;

		//Make sure the next number is in range
		while (nextGuess < 0 || nextGuess > 1000)
		{
			cout << "Error! Please pick a number between 1-1000!" << endl;
			cin >> nextGuess;
		}

		//Figure out if guess is closer (warmer) or further away (colder)
		//from the previous guess

		distance = distanceToClosest(solutions,SIZE,guess);
		secondDistance = distanceToClosest(solutions, SIZE, nextGuess);

		if (nextGuess == solutions[i])
		{	cout << "Correct!" << endl;
		return 0;
		}
		else if (distance == secondDistance)
		{	cout << "Your guess is the same distance away as your last!" << endl;
		}

		else if (distance > secondDistance)
		{	cout << "Getting Colder!" << endl;
		}
		else if (distance < secondDistance)
		{	cout << "Getting Warmer" << endl;
		}

		//You must change the value of lastGuess before re-running loop
		nextGuess = guess;

	}
	//end loop if correct
	while (distance != 0);
	}
	return 0;
}
//Determine the distance of the guess to closest solutions
int distanceToClosest (const int solutions[], int SIZE, int guess)
{
int diff[SIZE];
for (int i = 0; i < SIZE; i++)
{
	diff[i] = abs(solutions[i] - guess);
}
int lowest = diff[0];
for (int k = 0; k < SIZE; k++)
{
	if (lowest > diff[k])
	{
		lowest = diff[k];
	}
}
return lowest;
}
Topic archived. No new replies allowed.