Home > Blockchain >  How to change 1 or 0, into a true or false output
How to change 1 or 0, into a true or false output

Time:11-28

I have this hw assignment that I have completed. I am using a bool value in order for it to print 1 or 0. My question is how do I make it print out true or false, instead of the 1 or 0.

My output:

1 = true, 0 = false
------------
c1 >= c2 : 1
c1 <= c2 : 0
c1 != c2 : 1
c1 < c2 : 0
c1 > c2 : 1
c1 == c2 : 0

My code

int main() 
{

    //Declearing the radii of 2 circles in order to compare
    Circle c1(7);
    Circle c2(6);



    //prints true or false in boolean so 1 or 0

    bool compare;
    cout << "1 = true, 0 = false" << endl;
    cout << "------------" << endl;

    compare = c1 >= c2;
    cout<< "c1 >= c2 : "<< compare << endl;

    compare = c1 <= c2;
    cout<< "c1 <= c2 : "<< compare << endl;

    compare = c1 != c2;
    cout << "c1 != c2 : " << compare << endl;

    compare = c1 < c2;
    cout<< "c1 < c2 : " << compare << endl;

    compare = c1 > c2;
    cout << "c1 > c2 : " << compare << endl;

    compare = c1 == c2;
    cout << "c1 == c2 : " << compare << endl;
    cout << endl;
    

}

This is a multiple file program so I only added the main.cpp. Any advice would be greatly appreciated.

CodePudding user response:

It looks like we have some good options (ordered by time of submission):

  1. std::cout << (compare ? "true" : "false");
  2. std::cout << std::boolalpha << compare;
  3. const char *s[] = { "false", "true" }; std::cout << s[compare];
  •  Tags:  
  • c
  • Related