• Advertisement

Archived

This topic is now archived and is closed to further replies.

Why dosen´t this work?

This topic is 5616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why dosen´t this code work? if( hr == ( i || j ) ) While this does if( hr = i or hr = j ) ) It works in VB, so why not in C++?

Share this post


Link to post
Share on other sites
Advertisement
See my response in the other thread you created for why it doesn''t work; logical-or can return ANY nonzero value for "true".

I doubt extremely much that this works in VB; it''s much more likely that it happened to work because of the particular numbers you were using.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by peter86
Why dosen´t this code work?
if( hr == ( i || j ) )


post your code here. what is i and j? what do you mean it doesn''t work? it returns FALSE?

quote:

While this does
if( hr = i or hr = j ) )
It works in VB, so why not in C++?


never ever use (hr = i) in an if statement if you are comparing values. Equal sign (=) in C++ is used only as an assignment operator, not comparison. Use == for comparison.
Btw..what does "or" doing there? Are you sure it''s there?


My compiler generates one error message: "does not compile."

Share this post


Link to post
Share on other sites
Well, err, what are you trying to do?

If hr, i, and j are booleans, then it would make sense. Otherwise, nuh-uh.

Your second example has the obvious error in that h = i is always going to be true because you''re using the equal operator, not the comparison operator, and unless something is REALLY wrong, equal operators will always be successful.

-fel

Share this post


Link to post
Share on other sites
quote:

While this does
if( hr = i or hr = j ) )
It works in VB, so why not in C++?



Quite frankly because C++ is not VB. Also that code will not work (AFAIK, there is no ''or'' keyword in C++ and you have an ''orphan'' bracket at the end of the if statement). Also be aware that the single ''='' denotes assignment in C++, while the double ''=='' denotes comparison (which is a relatively easy thing to shoot yourself in the foot with).

The logical or operator ''||'' in C++ determines a true or false condition of the operands being compared. If one (or both) of the operands are a non zero value, it will return true otherwise it will return false.

If you change it to:

if(hr == i||hr == j) {
//Conditional code here
}

...you should get the results you expected.

HTH,

SysOp_1101

Share this post


Link to post
Share on other sites
They are entirely two different things. Of course the results are different.

In VB:
"OR" means "|" in C
"AND" means "&" in C
(hope i''m not wrong... many years not using VB)


Share this post


Link to post
Share on other sites
I know it should be ''=='' instead of ''='', my brain were temporary offline when i wrote it .

Wouldn´t this work either?:

if( i = ( j | k ) )

Share this post


Link to post
Share on other sites
Like felisandria said: only if hr, i, and j are booleans.

bool hr, i = true, j = false;

( i || j ) is then an expression (true) which can be compared to (or assigned to) hr.

However, I will assume HR is an HRESULT, which is of course not boolean. I understand it's not how you would write it in english, but it's how it is in c++ To do what you want to do you would have to write it as igamemakr and others proposed.



[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on December 3, 2002 5:19:14 PM]

Share this post


Link to post
Share on other sites
Leaving aside your snippets of code from 2 different langs...

What your logic is saying is:

IF hr EQUALS j OR hr EQUALS i THEN (statements)

Here''s the C++ code for it.
if((hr == i) || (hr == j))
{

}

|| is the logical OR. | is the binary OR (OR''ing 2 bits together)

FYI && is logical AND, & is binary OR.


V''lion

Bugle4d

Share this post


Link to post
Share on other sites

  • Advertisement