Problems with guessing game assingment

Hey I'm doing this project for a class. I'm suppose to be making a guessing game but I keep getting the same errors. Every time I run the program I get an infinite loop with the error message for the game. I think the problem is with the pointers but I can't figure it out. Any help would be appreciated!

#include <iostream>
#include <cmath>

using namespace std;



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




int main()
{
int solutions[5] = { 42, 142, 342, 442, 710 };
int currentCloseness = 0, lastCloseness = 0, guess = 0, lastGuess = 0;
const int SIZE = 5;

cout << "\t--Number Guessing Game-- \n";
cout << "Try to guess a numbers between 1 and 1000 \n";
cout << "Please make your guess: ";
cin >> guess;

while (guess < 0 || guess > 1000)
{
cout << "Please enter an number between 1-1000: ";
cin >> guess;
}

bool win = determineIfWinner(solutions, guess);
if (win == true) {
cout << "You got it on our first try! Good Job! \n";
return 0;
}
else
{
cout << "Sorry, That is incorrect! \n";
}

do
{

cout << "Please make a guess: ";
cin >> guess;

while (guess < 0 || guess > 1000)
{
cout << "Please enter a number between 1-1000: ";
cin >> guess;

}

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";

lastGuess = & guess;
lastGuess = * guess;

} while (currentCloseness != 0);



return 0;
}

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';

return closeness;

}

bool determineIfWinner(int solutions[], int guess)
{
for (int count = 0; count < 5; count++)
{
if (guess == solutions[count])
return true;
}

return false;
}
Its suppose to show if you're getting closer or further from the number
Its not the compiler error message its the error message asking the user to enter a new number .
lastGuess is the pointer
So would I set it equal to guess?
To make lastGuess equal to guess so the pointer knows where to get the integer from
@godofwar53
I compiled your code and it gives me 2 errors.

- error: invalid conversion from 'int*' to 'int' [-fpermissive] lastGuess = & guess;
Cannot convert pointers to integers.

- error: invalid type argument of unary '*' (have 'int') lastGuess = * guess;
Invalid syntax.


Try fixing these and tell me what happens. BUT if for some reason you don't get this error and it isn't your problem then I got another solution:

1
2
3
4
5
while (guess < 0 || guess > 1000)
{
    cout << "Please enter a number between 1-1000: ";
    cin >> guess;
}

Change this to:

1
2
3
4
5
6
7
while ((guess < 0 || guess > 1000) || cin.fail())
{
    cout << "Please enter a number between 1-1000: ";
    cin.clear();
    cin.ignore(1000000, '\n');
    cin >> guess;
}


Explanation:
Since the buffer in cin is not cleared, cin will always be invalid. So you need to clear it with "cin.clear();" and "cin.ignore(1000000, '\n');". (always use these 2 statements when checking for errors)

And you should also add in the conditions this: cin.fail()
This detects if the input is not the corresponding type with the variable you want to store the input in.

In all parts of your code that checks for user input error add the 2 statements and use cin.fail().
Last edited on
godofwar53,

I put your code in VS2015 and the errors I received dealt with
1
2
lastGuess = & guess;
lastGuess = * guess;

you are trying to store the address of guess in lastGuess which is an int not a pointer. And in the second line you are trying to dereference guess which is an int not a pointer. Both lines caused an error. After I commented them out the program compiled and I think it ran fine. Still not sure yet how all of it works.

Like me you are new to the forum. You need to use the <> to surround your code so it is easier for all of us to read. You can use the edit and highlight the code in your original message and press the <> to the right.

@boost lexical cast has a good answer.

Hope that helps,

Andy
Topic archived. No new replies allowed.