Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info:
To do: Test the bool data type - true/false, 0/1
To show: The use of bool data type
Code:
// Playing with bool, true, and false.
#include <iostream>
using namespace std;
// user defined function
bool func()
{
// Function returns a bool type
return NULL;
// NULL is converted to Boolean false, same
// as statement 'return false;'
}
int main()
{
bool val = false; // Boolean variable
int i = 1; // i is neither Boolean-true nor Boolean-false
int g = 5;
float j = 3.02; // j is neither Boolean-true nor Boolean-false
cout<<"Given the test value: "<<endl;
cout<<"\nbool val = false "<<endl;
cout<<"int i = 1 "<<endl;
cout<<"int g = 5 "<<endl;
cout<<"float j = 3.02 "<<endl;
cout<<"\nTESTING\n";
// Tests on integers
if(i == true)
cout<<"True: value i is 1"<<endl;
if(i == false)
cout<<"False: value i is 0"<<endl;
if(g)
cout << "g is true."<<endl;
else
cout << "g is false."<<endl;
// To test j's truth value, cast it to bool type.
if(bool(j) == true)
cout<<"Boolean j is true."<<endl;
// Test Boolean function returns value
val = func();
if(val == false)
cout<<"func() returned false."<<endl;
if(val == true)
cout<<"func() returned true."<<endl;
// false is converted to 0
return false;
}
Output:
Given the test value:
bool val = false
int i = 1
int g = 5
float j = 3.02
TESTING
True: value i is 1
g is true.
Boolean j is true.
func() returned false.
Press any key to continue . . .