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";
}
}
Printable View
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";
}
}
Code:if (a=true) // should be ==
welcome to c++ gotchas 101 :D
thanks now it works :D
if a number bigger then 1 is true, why writing for example 8 it print false?
but it print false...
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
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";
}
}
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:
Code://...
if(a)
//...
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?
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";
@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.
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?
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.Code:#include <iostream>
int main ()
{
bool a;
int value;
std::cout << "insert 0 or 1:";
std::cin >> value;
a = (value?true:false);
}
Regards,
Paul McKenzie
What compiler had you used? I get the same behavior with Visual Studio 2008. I would have thought that the integral value should get implicitly converted to a boolean. In fact when I debugged the code in VC++, I do see that it is able to get the value of the user input (say 8) but then there is a certain check which then fails and results in setting of the failbit of the stream. If you check the status of the cin stream post that read, you should see the failbit set which means the read had failed.
VC++ code (file: xlocnum, function: do_get)
The check _Ulo <= 1 is what fails. _Ulo is an unsigned long which does get successfully evaluated to 8 (the input). So, it is basically only allowing inputs as 0 and 1 via the stream.Code:{ // get zero or nonzero integer
char _Ac[_MAX_INT_DIG], *_Ep;
int _Errno = 0;
const unsigned long _Ulo = ::_Stoulx(_Ac, &_Ep,
_Getifld(_Ac, _First, _Last, _Iosbase.flags(),
_Iosbase.getloc()), &_Errno);
if (_Ep != _Ac && _Errno == 0 && _Ulo <= 1)
_Ans = _Ulo;
}
As has been written above, it is incorrect to say that a bool can only be assigned a value "true" or "false". Whatever value you try to assign, for example, an integral value say != 0, that value should get converted to a boolean and then that value set up as the value of the bool. So, it should be perfectly legal to do that.
Could this be a bug in VC++? I am not sure but I would have thought that it would have considered any non-zero value as true. Because, if I just simply set the value of boolean as 8 instead of reading from the stream, the automatic conversion rules kick in and the code works as expected. I don't have any other compiler but can anyone check what behavior is there with g++ or any other compiler?
Possibly the explanation is that streams work differently than operator =.
I don't have the standard in front of me, but, I know that other types will do things differently if you use input streams as opposed to using assignment. A clear example of the different behaviour is char arrays. Arrays cannot be assigned to, but a stream is able to assign characters to a char array.
Regards,
Paul McKenzie
Why when I cout characters like è,ç,ò,ì aren't interpretated correctly?
What can I do?
thanks!
When you cout characters like that they are coded in a way that does not correspond to the character coding used by the console.
If you use MBCS settings in your projects the "easiest" way out (unless you can use MFC/ATL http://msdn.microsoft.com/en-us/libr...a3(VS.80).aspx) is to open a console, start edit.exe, type the characters you want to use and then save the file. Now open the file using a hex-viewer and use the hex values instead of the original ones.
Just as a demonstration, I created a file (saved as ANSI) containing the swedish characters åäöÅÄÖ using notepad, then I re-opened the same file in a console using edit and added the same character string on a new line. The content of the file is now:
Now, if I would like to cout the swedish text "Sånt här är irriterande!" (These things are annoying!) in a console I would have to doCode:In edit in notepad in hex
σΣ÷┼─╓ åäöÅÄÖ E5 E4 F6 C5 C4 D6
åäöÅÄÖ †„”Ž™ 86 84 94 8F 8E 99
but the string would only be properly presented if the default codepage for the console is set to one that print swedish characters from my hex values.Code:cout << "S\x86nt h\x84r \x84r irriterande!"
To set the code page use SetConsoleCP (console reference http://msdn.microsoft.com/en-us/libr...87(VS.85).aspx)
Coding the output like in the above code is not very practical. A slightly better way of doing it could be likeCode:#define å "\x86"
#define ä "\x84"
#define ä "\x84"
cout << "S"å"nt h"ä"r "ä"r irriterande!" << endl;
why is necessary write them in hex?
if I do this, will not work in other computers, right?