Advertisement

[C++] How do I get this program to print true or false

Started by March 03, 2009 05:13 PM
10 comments, last by ToohrVyk 15 years, 10 months ago


#include <iostream>
int main()
{
    using std::cout;
    using std::endl;

    bool result = (4 < 5);
    cout << "4 < 5: " << result << endl;
    system("Pause");
    return 0;
}


#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;}
Advertisement
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.
Advertisement
If you want it to print True and False literally just do something like:

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 org
Also many programmers define TRUE and FALSE like this

#define TRUE 1
#define FALSE 0
This 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.

[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
Quote: Original post by ToohrVyk
Quote: Original post by Hazard org
Also many programmers define TRUE and FALSE like this

#define TRUE 1
#define FALSE 0
The only situation where doing so isn't stupid is when interacting with C code.


Actually, it is. C has stdbool.h.

This topic is closed to new replies.

Advertisement