i use an older compiler, borland c++ 3.1 and it doesn't have the bool variable type so i made my own bool type

enum bool
{
        false = 0, true
};

but always when i try to make comparisons or other thing i get the warning ASSIGNING INT TO BOOL

eg.

int main()
{
	bool x, y = true;
	x = false;
	if( x == false )
		x = true;
	if( x == true )
		x = false;
	x = x || y;
	if( ( x || y ) == true ) // here i get ASSIGNING INT TO BOOL
        	x = false;
	return 0;
}

how could i get rid of this warning ? they don't affect the program , but they are annoying

Recommended Answers

All 18 Replies

( x || y ) is an expression of type bool (at least I think so - even though you can't declare variables of type bool it still exists as a type) true is of type int. I believe this comparisan of int to bool is causing the issue (though the error you gave would suggest otherwise, since there is no assignment in the example above)

simply remove the == true. It's unnecessary, not just in this case but in all cases. (x || y) is already of type bool. there is no need to add the == to make it a bool.

u cant use bool in borland ......... it will not support u here use turbo c++

u cant use bool in borland ......... it will not support u here use turbo c++

you didn't understand, i said that i made my custom bool type

During "pre-bool period" of C++ language (x || y) was an expression of type int (not bool), as in C language. Its possible values were 0 or 1 (by definition).

Probably, in (x || y ) == true expression the venerable elder (BC++ 3.1) made an attempt to convert int value of (x||y) to "enum bool" (named "bool" in C++) to compare with "true" "enum bool" constant. In modern C++ it's impossible. Many years ago it was only doubtful operation in C++ - so the compiler confined himself to a short warning message.

Moral#1: Avoid obviously bad style a la "boolean_expression == true (or false)" (follow CoolGamer48's advice).

Moral#2: Never, never use bool, true and false identifiers in your C++ programs, even if you have a very old compiler...

Moral#2: Never, never use bool, true and false identifiers in your C++ programs, even if you have a very old compiler...

Wait - you're saying to not use variables of type bool?

Don't worry: bool, true and false IDENTIFIERS - names of program entities (types, variables, functions etc). Now they called keywords or reserved words in C++.

It's my attempt to warn NickyU against hasty decorations of C++ sources...

I don't get it. I too use Turbo C++. I copied the code & run it. It had no problem(No Warning No error). But in Code::Blocks, Since bool was already known I had the error message. And then || operator returns integer as far as I know. returns 1 if true & 0 if not

sorry, a little mix up. I used Turbo C compiler instead of turbo C++ which is strict when it comes to type checking. You just have to convert the int returned by x||y to bool x = (bool) (x || y);

i tried just to make my code writing more efficient, i also used dev c++ a lot so i got used with bool

i understand that my compiler returns this: ( x || y ) as an int type, so it automatically converts it to the custom bool type i made, but the ASSIGNING its just annoying, i don't understand why it is a problem

Its kind of strange. In my turbo c++ compiler I got that warning for the line x = x||y ; Besides, you don't assign anything in the if statement.

Warning D:\TC\BIN\NAME.CPP 14: Assigning int to bool in function main()

Line 14: x = (x || y);
As I said before type casting would do.

i cant type cast with this compiler

What? Never heard of that. Don't tell me,
this don't work
x = bool (x||y) ;

I don't understand why it says assigning either. If it said casting or converting or something like that then I would get it. Because true really isn't of type int, it's of type enum bool, so when comparing it to an int there has to be an implicit cast or conversion, which is probably what the warning is about.

I agree with you CoolGamer & refer to my post. I didn't get that warning in that line. It was the line before it, that's why I suggested a type cast.
x = x||y ; x||y will return ( false||true == 0||1 == 1 ) This int is being assigned to the type bool hence the warning.

In modern C++ operator || returns bool, not integer!

Don't test against true. Test against false.

That is the danger in defining values for true and false. Just remember, always (and this is one of 'those' rules...) explicitly test against false. Only implicitly test for true:

if (x != false) yah();
if (x) yah();

if (x == false) fooey();
if (!x) fooey();

If your compiler still complains, just do an explicit cast:

if ((bool)(x || y)) yah();

Good luck!

What? Never heard of that. Don't tell me,
this don't work
x = bool (x||y) ;

That is C++ only. The C way looks like this.

x = (bool)(x || y);

Perhaps the OP is compiling as C instead of C++.

As for the original code, it would work best with a typedef, in case you actually wanted to declare variables of type bool.

typedef enum
{
        false, true
} bool;

(With the original code one would have to use enum bool x; to declare "bool"s.)

BTW, I'm sure Turbo C doesn't have it, but in C99 there's a header file called stdbool.h that gives you bool, true, and false.

it is some work to write (bool) in front, but al least no errors :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.