bool functions and comparing their return values in main()

Hi
This is my first post here, and ill give some little info about me.
Recently after long term thinking i decided to follow the programmers step since i love to solve problems and mostly the math ones. As well i like create.. i have to say so fur i have enjoyed learning c++.
2 weeks ago i started to learn c++ from a book "Michael Dawson - Beginning C++ Through Game Programming", but as in every book there is no answer to every question.

Now i gave my self a task to recreate the game Tic Tac Toe from the book.
The first time i went really far but somewhere in the middle of the project i got myself stuck and i decided to begin again.
With cleaner pseudo planning and more sophisticated code.

However now i am stuck with this (not enough experience with the c++ syntax so far..) syntax problem.

ok .. so this is my code:
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
#include <iostream>	
#include <vector>
#include <string>
using namespace std;

bool doYouWantToPlayFirst(string question);





int main ()
	{
	char humanPiece;
	char computerPiece;
	

	doYouWantToPlayFirst("Do you want to take the first move? <y/n>: ");
        // now here the problems lies! ill explain what i tried after the code finishes....	
         if (doYouWantToPlayFirst("Do you want to take the first move? <y/n>: ") == true)
		{
		humanPiece = 'X';
                computerPiece = 'O';
		}
          else 
                {
                humanPiece = 'O';
                computerPiece = 'X';


	return 0;
	}




bool doYouWantToPlayFirst(string question)
	{
	char first;
	cout << question;

	do
		{
		cin >> first;
		first = toupper(first);
			if ((first != 'Y') && (first != 'N'))
				{	
				cout << "\nEnter 'y' for Yes or 'n' for NO! <y/n>: ";
				}
		}while ((first != 'Y') && (first != 'N'));

		if (first == 'Y')
			{
			cout << "\nYou got the first move!\n\n";
			return true;
			}

	
		if (first == 'N')
			{
			cout << "\nOk, you shall feel the wrath of my AI brain!!\n\n";
			return false;
			}
	}



so when i call the function like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ");
	if ((doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ")) == true)
		{
		humanPiece = 'X';
		computerPiece = 'O';
		}
	else
		humanPiece = 'O';
		computerPiece = 'X';
	cout << humanPiece ;

	return 0;
	}

it actually works! But it calls the function twice really (asks the users twice for <y/n> from the first time i call the function and the second time in the if (statment where i just want to compare it).

If i try to call the function like this:
 
if (doYouWantToPlayFirst() == true)

it says that the function doesn't take 0 arguments.. so for the doYouWantToPlayFirst(string question) i must pass a value.. ok i understand that.. but how (is it actually possible to only use the return value of the function..
as simple as this: if doYouWantToPlayFirst (returned TRUE) to make something else...

Later on i have found a solution to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
	{
	char humanPiece;
	char computerPiece;
	
	
	if (doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ") == true)
		{
		humanPiece = 'X';
		computerPiece = 'O';
		}
	else
		humanPiece = 'O';
		computerPiece = 'X';
	cout << humanPiece ;

	return 0;
	}

as you can see in this code i actually only called the function directly in the if statement. But i would still like to know if there is some other way.
Thanks again.
p.s. so far the use of functions are the most confusing in bigger codes (cause i am still not used to them) its actually a bit overwhelming but i DO NOT INTEND TO GIVE UP... like ever ;)

Thanks.
This is actually the best way in my opinion. Very tidy.

If you want to have less information in the "if" statement then you could try the following by adding another label:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
	{
	char humanPiece;
	char computerPiece;
	
	bool PlayFirst = doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ");
	if (PlayFirst)
		{
		humanPiece = 'X';
		computerPiece = 'O';
		}
	else
		humanPiece = 'O';
		computerPiece = 'X';
	cout << humanPiece ;

	return 0;
	}


Or you could use: (no '==true' required)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
	{
	char humanPiece;
	char computerPiece;
	
	if (doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: "))
		{
		humanPiece = 'X';
		computerPiece = 'O';
		}
	else
		humanPiece = 'O';
		computerPiece = 'X';
	cout << humanPiece ;

	return 0;
	}
I suppose you could also use a string variable in the name:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
	{
	char humanPiece;
	char computerPiece;

	string Question = "Dali sakas da si prv na poteg? <y/n>:";
	if (doYouWantToPlayFirst(Question))
		{
		humanPiece = 'X';
		computerPiece = 'O';
		}
	else
		humanPiece = 'O';
		computerPiece = 'X';
	cout << humanPiece ;

	return 0;
	}


Or you could just move the question to the function so that the function doesn't take any arguments at all. Just Cout the string directly (unless you want to call this function a few times with different questions).
thanks for the sugguestions, i know there are other ways to do this.
I actually came with this couple of mins ago:

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
// Iks Tocka (majke mu)
#include <iostream>	
#include <vector>
#include <string>
using namespace std;

bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece);





int main ()
	{
	char humanPiece;
	char computerPiece;
	
	 doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ", humanPiece, computerPiece);
		cout << endl <<  humanPiece << "  " << computerPiece;


	cout << endl << endl;
	system ("PAUSE"); // system pause
	return 0;
	}




bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece)
	{
	char first;
	cout << question;

	do
		{
		cin >> first;
		first = toupper(first);
			if ((first != 'Y') && (first != 'N'))
				{	
				cout << "\nVnesi bukva 'y' za DA ili 'n' za NE! <y/n>: ";

				}
		}while ((first != 'Y') && (first != 'N'));

		if (first == 'Y')
			{
			cout << "\nTaka e mrshav hjumanoidu, mora da bides prv za da ne places! No ne e vazno!! \nJas pak ke pobedam!!! Moram da pobedam.... err biip biiep ..... \nZboruva tvojot kompjter!\n\n";
			humanPiece = 'X';
			computerPiece = 'O';
			return true;
			}

	
		if (first == 'N')
			{
			cout << "\nDobro mrshav hjumanu! Pripermi se jas ke napdnam prv so seta sila!!!\n\n";
			humanPiece = 'O';
			computerPiece = 'X';
			return false;
			}
	}


with the use of reference i change the humanPiece and the computerPiece.
But my real question lies elsewhere.. not in the solution actually..
I am wondering if there is a way in the c++ language to actually CHECK the returned value from a function in this example is bool so true or false.. but without using other variables as we used.. i want to know if there is direct approach to this without using references to other vras and stuff like that..
Thanks though.
masky007 wrote:
I am wondering if there is a way in the c++ language to actually CHECK the returned value from a function...

When you code:
1
2
3
4
5
6
7
8
9
10
11
12
bool someBoolFunc(...);

int main() {
    ...
    if( someBoolFunc(...) ) {
        ...
    } else {
        ...
    }
    ...
}
...
that is exactly what you are doing. The return value is being passed to the if statement... unless I misunderstood. When you say "check the value" do you mean print it to screen like with a string or int? If so, then yes.
1
2
3
4
5
6
7
8
bool someBool = ...;
cout << someBool; //will print "0" if false and "1" if true
cout << (someBool ? "true" : "false"); //will print "false" if "false" and "true" if "true"
                                       // This may look weird to you, but it is just about the same as typing
if( someBool )
    cout << "true";
else
    cout << "false";
yes the returned value is passed to the if statement... i know that.. BUT
What happens when you call a function in a if statement is actually really "calling" the function.

here is one example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece)
	{
	char first;
	cout << question;

	do
		{
		cin >> first;
		first = toupper(first);
			if ((first != 'Y') && (first != 'N'))
				{	
				cout << "\nPlease enter Y or N <y/n>: ";

				}
		}while ((first != 'Y') && (first != 'N'));

		if (first == 'Y')
			{
			cout << "\nOK week human, take the first move, ill beat ya anyways!";
			humanPiece = 'X';
			computerPiece = 'O';
			return true;
			}


in this peace if code if i call it in a if statement than ...
It will not only check if == true or false but it will as well prompot the user to enter for "cin" that i have in the function.. as well will update the other functions... so my question is. Is there is a way "only" and only to get the returned value of that function without calling the whole function.. in this case.. i need only true or false . and not prompt the user to enter <y/n>


Something like this (tough this is illegal)..
1
2
3
4
5
6
7
8
9
10
/*this is how should the function be called in the main() function ... actually i mean.. what i want to exist :P) but i am begining to understand now that this is highly impossible but maybe there is some way*/


if (doYouWantToPlayFirst(returned value only) == true
{
some statement;
}
else
{
some statement;
Last edited on
Is there is a way "only" and only to get the returned value of that function without calling the whole function
¿What value are you expecting as a result?
¿the first user response? In that case you need to cache it.

Please don't do == true, it's superfluous and confusing
in this cause because the function is bool i expect it to return true or false ofc.
and what do you mean be caching it?
how do you cache the value of the returned function (in this case is true or false)

ne555 sorry if i am not clear enough (trying my best, ill try again to explain very shortly)

I simply want to put the function returned value (true or false) in a if statment BUT without calling the whole function..

in example:

1
2
3
4
5
6
7
8
9
10
11

bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece); // here the user is asked if he wants to play first or not.. he answers 'y' or 'n' and the value returns true for 'y' or false for 'n'

//now i want to only check what the users has answered (dose the function the first time returned true or false) so i do this

if (doYouWantToPlayFirst("Do you want to play first? <y/n>: ", humanPiece, computerPiece) == true) 
{
some statement;
}
/*but the problem here is that i prompt the user again with this... it actually checks the returned value of the bool function (true or false) but as well it calls the whole function again and prompt the user once more time to enter y/n... while this time i only need to see if earlier in the code was returned true or false.
 */

So what is the best solution to this problem?
I know i can save the answer earlier in the function in a global variable or to pass it with & so i could access and change it (and put that variable in if statement instead if the whole function) .. (that's what i did in my program) but is there anything more sophisticated than that? like to check what was the last time returned value of the specified function.
thanks
Last edited on
put that variable in if statement instead if the whole function
That is the best solution. When you call a function, you expect that the function will be called.

Look at one alternative (this doesn't considerer the parameter, checkout std::map for that)
1
2
3
4
5
6
7
8
9
10
bool faq(string question){ //the piece assignment shouldn't be done here
  static bool first_time=true, cache;
  if( first_time ){
    first_time=false;
    //all the logic
    if (first == 'Y') cache=true;
    else cache=false;
  }
  return cache;
}


because the function is bool i expect it to return true or false ofc.
That is why I suggest you to use the value directly.
if(var) if( not var ) instead of if( var==true ) if( var==false )
Last edited on
Thanks sir that was helpful.
Topic archived. No new replies allowed.