How to make bool cout yes or no

How would I even go about this? It couts the right values such 1 or 0. I want to know if I can "convert" the 1's and 0's to yes or no.
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include <string>
#include <iostream>
#include <ostream>
using namespace std;

class Vehicle
{
	private:
		int age;

	protected:
		float price;

	public:
		Vehicle(){age=0;price=0.0;}
		~Vehicle(){age=0;price=0.0;}
		void setAge( int x) {age=x;}
		void setPrice(float x){price=x;}
		int getAge(){return age;}
		float getPrice(){return price;}

};

class Car: public Vehicle
{
	public:
		bool RaceCarStatus;
		void setRaceCarStatus(bool y){RaceCarStatus = y;}
		bool  getRaceCarStatus(){return RaceCarStatus;}

		Car(){RaceCarStatus=false;}
		~Car(){RaceCarStatus=false;}
		Car(const Car & y);
};

class Truck:public Car
{
	private:
		bool DieselTypeStatus;

	public:
		Truck(){DieselTypeStatus = false;}
		Truck(bool z){DieselTypeStatus=z;}
		~Truck(){DieselTypeStatus = false;}
		void setDieselTypeStatus(bool z){DieselTypeStatus = z;}
		bool getDieselTypeStatus(){return DieselTypeStatus;}
};

void main()
{
	Vehicle x;
	cout <<"Initial value for x: " << endl;
	cout <<"Age = " << x.getAge()<<"  "<< "Price =$" << x.getPrice()<<endl;
	x.setAge(40);
	x.setPrice(20000);
	cout << "Modified value for x: " << endl;
	cout << "Age = "   << x.getAge()<<"  "<< "Price =$" << x.getPrice()<< endl;
	cout<<"---------------------------"<<endl;


	Car y;
	cout <<"Initial value for y:"<<endl;
	cout <<"Age = " << y.getAge()<<"  "<<"Price = $"<<y.getPrice()<<endl;
	cout <<"Race Car?"<<y.getRaceCarStatus()<<endl;
	y.setAge(50);
	y.setPrice(15000);
	y.setRaceCarStatus(true);
	cout << "Modified value for y:"<< endl;
	cout << "Age = " << y.getAge()<<"  "<< "Price = $"<<y.getPrice()<<endl;
	cout << "Race Car? " <<y.getRaceCarStatus()<<endl;
	cout<<"---------------------------"<<endl;

	Truck z;
	cout <<"Initial value for z:"<<endl;
	cout <<"Age = " << z.getAge()<<"  "<<"Price = $"<<z.getPrice()<<endl;
	cout <<"Race Car?"<<z.getRaceCarStatus()<<endl;
	cout << "Needs diesel?"<<z.getDieselTypeStatus()<<endl;
	z.setAge(20);
	z.setPrice(30000);
	z.setRaceCarStatus(false);
	z.setDieselTypeStatus(true);
	cout << "Modified value for z:"<< endl;
	cout << "Age = " << z.getAge()<<"  "<< "Price = $"<<z.getPrice()<<endl;
	cout << "Race Car? " <<z.getRaceCarStatus()<<endl;
	cout << "Needs diesel?"<<z.getDieselTypeStatus()<<endl;


system ("pause");

}


Not sure if the complete code was needed but I provided it anyways. Just to give an idea of what I am doing. I also saw that there was another post for the exact same answer. It wasn't helpful.
Last edited on
hey you can do it like this:

cout << "Race Car? " << z.getRaceCarStatus() ? "True" : "False" <<endl;
closed account (Dy7SLyTq)
the short answer: no you cant. this isnt python. i dont think c++ actually knows what a string is (someone correct me if im wrong.) and it is only emulated with std::string and char *'s (traditionally).

the long answer: no but you can pretend you can, using the terenary operator (?:) which if you know how to do ifs in excel then you can do this. how it works:
condition ? value_if_condition_is_true : value_if_false

for example: cout<< RaceCarStatus ? "yes" : "no"
=================================================
as to your code: why are you including iostream and ostream? ostream is included with iostream
also why do you have a getter and a setter in car when you make the member public anyways? it defeats the purpose of getters and setters

main should be return an int as is stated by the standard

you shouldnt get into habit of using system. system itself is a bad way to interface with the environment and there are better ways of pausing

edit: ninja'd...
Last edited on
This code is for a project I had to set up. I just include all headers because I don't want to get an error for not adding anything. Plus I was getting an error because I didn't include ostream. Okay you have a point about the car but we had to answer a question answering if we need to add getters/setters or not. So that's why its in there.
In addition to the already mentioned ?: operator...

<iomanip> has the boolalpha qualifier which will print it as "true" or "false":

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    bool example = true;

    cout << boolalpha << example; // prints "true"
}



Or you can use a LUT:

1
2
3
4
5
6
7
8
const char* boollut[2] = {"no", "yes"};

int main()
{
    bool example = true;

    cout << boollut[example];  // prints "yes"
}
+1 Disch :)
Or use a custom numpunct facet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <locale>
#include <string>

int main()
{
    struct custom_bool : std::numpunct<char>
    {
        custom_bool( const char* true_name, const char* false_name )
             : true_name(true_name), false_name(false_name) {}

        const std::string true_name ;
        const std::string false_name ;

        protected:
          std::string do_truename() const override { return true_name ; }
          std::string do_falsename() const override { return false_name ; }
    };

    const bool flags[] = { true, false, false, true } ;
    const auto print_bools = [&flags]
    { for( bool b : flags ) std::cout << b << ' ' ; std::cout << '\n' ; };

    print_bools() ;

    std::cout << std::boolalpha ;
    print_bools() ;

    std::cout.imbue( { std::cout.getloc(), new custom_bool( "yes", "no") } );
    print_bools() ;

    std::cout.imbue( { std::cout.getloc(), new custom_bool( "da!", "nyet!") } );
    print_bools() ;

    std::cout << std::noboolalpha ;
    print_bools() ;
}

http://coliru.stacked-crooked.com/a/e772f42ec8494a61
closed account (Dy7SLyTq)
@disch: what is a LUT?
@DTSCode lookup table
closed account (Dy7SLyTq)
ahhh that makes sense... thanks for that
Topic archived. No new replies allowed.