<< Previous

Tricky booleans

This time, the goal is to complete the empty function named createBool in the given code so that it prints the text You win! at the end. Simple, right?

The function createBool is feeded with the numbers 1 and 2, and the goal is to generate two different boolean values, such that both of them evaluate to true, but still not equal to each other. This is checked by the function check in the code. It will output the two booleans one by one, and then it will output their relation. If both are true and they are not equal, you win.

You have completed the puzzle if the output will be:

A = true
B = true
A != B
You win!

Good luck!

puzzle.cpp

#include <iostream>

using namespace std;

// Create magic boolean values that can cause the 'check' function to return true.
bool createBool(int id) {

	// Your code goes here!

}

// Convert a bool to a string for output.
const char *toS(bool b) {
	return b ? "true" : "false";
}

// Check so that a is true, b is true and a != b. Outputs the
// current status of a and b as well.
bool check(bool a, bool b) {
	bool equal = a == b;
	cout << "A = " << toS(a) << ", B = " << toS(b) << endl;
	if (equal)
		cout << "A == B" << endl;
	else
		cout << "A != B" << endl;
	return a && b && !equal;
}

// Check the implementation of 'createBool'.
int main() {
	bool a = createBool(1);
	bool b = createBool(2);

	if (check(a, b))
		cout << "You win!" << endl;
	else
		cout << "Try again!" << endl;

	return 0;
}

Download files

Answer and comments