Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 4th, 2008, 06:06 AM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default The boolean data type in C++?


When i assign a non-zero value to a boolean variable, the output is true. And when i assign a non-zero value from the console, the output is false.
WHY?

Please take a look at the following codes:
#include <iostream.h>
#include <conio.h>

main()
{
bool bln;
cout<<"Enter value: ";
cin>>bln;

if (bln==true)
{
cout<<bln<<"TRUE";
}
else
{
cout<<bln<<"FALSE";
}
getch();
}

Output:
0FALSE


/*************************************...
#include <iostream.h>
#include <conio.h>

main()
{
bool bln=253;


if (bln==true)
{
cout<<bln<<"TRUE";
}
else
{
cout<<bln<<"FALSE";
}
getch();
}

Output:
1TRUE

MAXOOD!

Life is an endless journey towards perfection
__________________
MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #2 (permalink)  
Old May 10th, 2008, 02:55 AM
Friend of Wrox
 
Join Date: Mar 2008
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

i cant even compile it

Reply With Quote
  #3 (permalink)  
Old July 14th, 2008, 01:11 AM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well assigning values like 250 or some value from console to a bool does not make any sense to me and obviously it does not make any sense to compiler also..but i figured your problem if you havent yet when you try to get a value into a bool from console your attempt is ignored and your variable does not change at all..maybe writing directly into a bool is not allowed or you have to cast it first.when i initilaze bln = true and i enter 0 i get 1 true and when i initilize it first to false and enter a non-zero i get 0 false as output also when i dont initliaze it output is some junk value like 204 on my machine and false..you have to cast the value to bool like that cin>>(bool)bln; when i do that everything works just as expected..

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Data type samnachilomo SQL Server 2000 1 November 25th, 2006 03:25 PM
Conversion from type 'DBNull' to type 'Boolean' is steve35719 VB Databases Basics 4 June 29th, 2006 06:13 PM
data type kfir XML 6 May 7th, 2006 07:15 AM
Which data type should I use? logmaster C# 0 January 2nd, 2006 10:06 AM
Data Type error but it is the correct type Mitch SQL Server 2000 2 March 19th, 2004 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.