-
March 19th, 2011, 01:19 PM
#1
problem with boolean
Hi, I wrote this code but why writing 0 it print true instead of false?
Thanks!
#include <iostream>
int main ()
{
bool a;
std::cout << "insert 0 or 1:";
std::cin >> a;
if (a=true)
{
std::cout << "a is true";
}
else
{
std::cout << "a is false";
}
}
-
March 19th, 2011, 01:30 PM
#2
Re: problem with boolean
Code:
if (a=true) // should be ==
-
March 19th, 2011, 01:54 PM
#3
Re: problem with boolean
welcome to c++ gotchas 101
-
March 19th, 2011, 02:02 PM
#4
Re: problem with boolean
thanks now it works
if a number bigger then 1 is true, why writing for example 8 it print false?
-
March 19th, 2011, 02:11 PM
#5
Re: problem with boolean
Originally Posted by abc++
if a number bigger then 1 is true, why writing for example 8 it print false?
Any value other than 0 is convertible to true.
-
March 19th, 2011, 02:23 PM
#6
-
March 19th, 2011, 02:28 PM
#7
Re: problem with boolean
Originally Posted by abc++
but it print false...
Let's see your updated program.
And please use code tags when posting code.
Regards,
Paul McKenzie
-
March 19th, 2011, 03:40 PM
#8
Re: problem with boolean
See this FAQ. Int a nutshell, don't try to assign int values to a bool. Acceptable values are true and false.
http://www.codeguru.com/forum/showthread.php?t=332831
-
March 19th, 2011, 03:49 PM
#9
Re: problem with boolean
Excuse me I don't see the code button, however my updated code is this and typing a number bigger then 1 it print false:
Code:
#include <iostream>
int main ()
{
bool a;
std::cout << "insert 0 or 1:";
std::cin >> a;
if (a==true)
{
std::cout << "a is true";
}
else
{
std::cout << "a is false";
}
}
-
March 19th, 2011, 04:15 PM
#10
Re: problem with boolean
An bool in c++ is 1 byte (8 bits) if you store the value 7 in that byte and compare it with the value 1 (true) it will go on the false branch (because those values are not equal) even if they both evaluate to true.
You can modify the code like this and then you will get the desired behavior:
-
March 19th, 2011, 04:35 PM
#11
Re: problem with boolean
in input a bool must be only 0 or 1 ?
how can i write a text like "Hello \n" but considering \n a normal text?
-
March 19th, 2011, 04:42 PM
#12
Re: problem with boolean
No, that is what i tried to explain to you, a bool can store other values, since it is not a single bit (it's 8)
You need to double the \ to appear:
Code:
cout << "Hello \\n";
-
March 19th, 2011, 04:53 PM
#13
Re: problem with boolean
Originally Posted by Zlatomir
No, that is what i tried to explain to you, a bool can store other values, since it is not a single bit (it's 8)
You need to double the \ to appear:
Code:
cout << "Hello \\n";
It can, but it shouldn't. true and false are the two documented values you can assign it.
-
March 19th, 2011, 05:03 PM
#14
Re: problem with boolean
@GCDEF agree with you that it shouldn't, but it can and there can be some very tricky situations regarding this fact.
I talk about for example uninitialized bool which can return false if compared to true and again false if compared to false, so the programmer should know that the bool can store 8 bits not just 1.
//my bad that i didn't specifically said that it's not recommended to use bool for storing anything else than true or false.
-
March 19th, 2011, 07:46 PM
#15
Re: problem with boolean
Originally Posted by abc++
in input a bool must be only 0 or 1 ?
What is the purpose of having a bool as input if you want to store more values? Why not just make it an int instead of a bool?
Code:
#include <iostream>
int main ()
{
bool a;
int value;
std::cout << "insert 0 or 1:";
std::cin >> value;
a = (value?true:false);
}
Now there is no issue. Since you want 0 to mean false, and anything else to mean true, then this is guaranteed to work correctly.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|