#include <iostream>
int main()
{
using std::cout;
using std::endl;
bool result = (4 < 5);
cout << "4 < 5: " << result << endl;
system("Pause");
return 0;
}
[C++] How do I get this program to print true or false
#include <iostream>int main(){ using std::cout; using std::endl; bool result = (4 < 5); cout << "4 < 5: " << (result ? "true" : "false") << endl; // system("Pause"); // I can't get myself to leave this here. :) return 0;}
Ok, thanks for your reply. But isn't cout suppose to know that the type is of type bool and print "true" or "false". Did I find a bug or something? :)
No, the formatted insertion operator is supposed to print 1 for true and 0 for false for bools.
#include <iostream>#include <iomanip>int main(){ using std::cout; using std::endl; using std::boolalpha; bool result = ( 4 < 5 ); cout << "4 < 5: " << boolalpha << result << endl; return 0;}
EDIT:
Ninja'd!
Ok, thanks to everyone. Just curious...what book did you learn that from, _fastcall. I'm currently learning c++ and reading from sams teach yourself c++ in 21 days. I'm looking into getting other c++ books.
If you want it to print True and False literally just do something like:
Also many programmers define TRUE and FALSE like this
#define TRUE 1
#define FALSE 0
Accelerated C++
How to program in C++
The C programming language by K&R
Best regards,
Claudiu
if (4 < 5) cout << "True\n";else cout << "False\n";
Also many programmers define TRUE and FALSE like this
#define TRUE 1
#define FALSE 0
Accelerated C++
How to program in C++
The C programming language by K&R
Best regards,
Claudiu
Quote: Original post by Hazard orgThis is a C idiom. The C++ language provides its own bool type, which makes defining your own quite silly—the only situation where doing so isn't stupid is when interacting with C code.
Also many programmers define TRUE and FALSE like this
#define TRUE 1
#define FALSE 0
[embarrass] Very true, don't know what I was thinking of [lol]. I was thinking C when I said you can define them :) Thanks for pointing that out, must be something in the air that made me say that :P
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement