Trouble with Boolean Function

Hello again, I ran into another issue with a program that I cannot figure out. My program is supposed to be a guessing game. The number they should guess is 60 and if they guess too high it tells the user that it's too high and to try again. If they guess too low it tells the user that it's too low and to try again. My program will display the menu and allow me to type in a guess, but when I hit enter the program doesn't do anything else and gives me the "Press any key to continue" dialogue. Here is the code:

#include <iostream>
bool check(int theNumber, int userGuess);
using namespace std;
int main()
{
int userGuess;
int theNumber = 60;
do
{
cout << "I have a number between 1 and 100" << endl;
cout << "Can you guess my number?" << endl;
cout << "Please type your guess" << endl;
cin >> userGuess;
bool check(int theNumber, int userGuess);
} while (check == false);
}
bool check(int theNumber, int userGuess)
{
if (userGuess = theNumber)
{
return true;
}
if (userGuess < theNumber)
{
cout << "Too Low. Try Again.";
return false;
}
if (userGuess > theNumber)
{
cout << "Too High. Try Again.";
return false;
}
}

It should continue looping and using the check function until the check function is true. It is true when it is equal to 60. I just can't get it to do anything.
closed account (28poGNh0)
Try to figure out

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

bool check(int theNumber, int userGuess);

int main()
{
    int userGuess;
    int theNumber = 60;

    do
    {
        cout << "I have a number between 1 and 100" << endl;
        cout << "Can you guess my number?" << endl;
        cout << "Please type your guess" << endl;
        cin >> userGuess;
    } while (!check(theNumber,userGuess));
}

bool check(int theNumber, int userGuess)
{
    if (userGuess == theNumber)
    {
        cout << "You found it,great ,wonderfull,ahhhh you re the most inteligent person I know ,";
        cout << "please teach me how to do ,amazing what a smart boy ...!?!$" << endl;
        return true;
    }
    if (userGuess < theNumber)
    {
        cout << "Too Low. Try Again.";
        return false;
    }
    if (userGuess > theNumber)
    {
        cout << "Too High. Try Again.";
        return false;
    }return true;
}
closed account (48T7M4Gy)
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
#include <iostream>
bool check(int theNumber, int userGuess);
using namespace std;
int main()
{
	int userGuess;
	int theNumber = 60;
	bool match;///

	do
	{
		cout << "I have a number between 1 and 100" << endl;
		cout << "Can you guess my number?" << endl;
		cout << "Please type your guess" << endl;
		cin >> userGuess;
		match = check( theNumber, userGuess); ///
	} while (match == false); ///
}

bool check(int theNumber, int userGuess)
{
	if (userGuess == theNumber) ///
	{
		return true;
	}
	if (userGuess < theNumber)
	{
		cout << "Too Low. Try Again.";
		return false;
	}
	if (userGuess > theNumber)
	{
		cout << "Too High. Try Again.";
		return false;
	}
}
Last edited on
good lord! amazing and smart boy for 60th try!!
8 try is enough :P
Okay so I need to use a different boolean variable for the function and in the main block of code? Everything is working smoothly now. Thank you very much for the help.

Edit: Yeah I had to be very enthusiastic with the function when they got it right. Part of the assignment ;)
Last edited on
Topic archived. No new replies allowed.