The C and C++ code snippets monologue forum

The C, C++, Code, Coder, Coding Snippets Zone for Tenouk.com

 

 
It is currently Thu Sep 09, 2010 12:34 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Test the bool data type - true/false, 0/1
PostPosted: Sat Nov 11, 2006 8:04 am 
Offline

Joined: Sat Nov 11, 2006 2:09 am
Posts: 89
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 . . .

_________________
trapped yourself at honeypot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Skin by Lucas Kane