If equal don't do anything

Pages: 12
Okay, so I were updating my mini-rpg console app. thingy and I wanted the console to write "Draw!" if both heroes lose (their health below 0)

So how do it? Something like:

1
2
if (herohealth >= 0 and villianhealth >= 0) 
cout << "Draw!";

Now, how do I do it correctly?

Also, if I'd want to make something like that:

1
2
if (heroname >= 15 or == 30) 
and so on;


Now, how do I do it correctly?
By using the correct C++ operators. They're all here:

http://www.cplusplus.com/doc/tutorial/operators/

The ones you want are && and ||
and and or are valid C++ operator ( as well as && and || )
True, although they are considered to be the alternative rather than the primary.
Last edited on
closed account (zb0S216C)
@Moschops
As Bazzy said, they're still valid. It's like saying:
1
2
3
4
5
6
7
#define BEGIN {   // Doesn't change the way '{' operates.
#define END }     // Doesn't change the way '}' operates.

void Function( void )
BEGIN
    // ...
END

Last edited on
I wonder why they are not highlighted here.
1
2
3
4
//if (heroname >= 15 or == 30) 
if (heroname >= 15 or heroname == 30) 

if (herohealth >= 0 and villianhealth >= 0) //they are still alive 
Because they are basically macros AFAIK.
they can't be standard, are they in a certain header? I get undeclared identifier errors when I try that.
http://www.cppreference.com/wiki/language/operator_alternative
They do not expand as macros and vim, gedit, ZinjaI do highlight them.

Edit: ZinjaI only highlights and or not xor
Last edited on
firedraco wrote:
Because they are basically macros AFAIK.
quirkyusername wrote:
they can't be standard, are they in a certain header? I get undeclared identifier errors when I try that.

In C++ they are standard operators ( not macros ) see [lex.operators] in the ISO document.
In C they are standard macros defined in iso646.h http://www.cplusplus.com/reference/clibrary/ciso646/
1
2
3
4
5
6
if (mhr >= 0)
	cout << monster << " won the fight!\n";
		else cout << heroname << " won the fight!\n";

if (mhr >= 0 && hhr >= 0)
	cout << "Draw!\n";


Now, how do I make the program not print both "Monster won the fight" and "Hero won the fight" if the fight ends in a draw?
Your condition is upside-down. You should test if both are negative
1
2
3
4
5
6
if( both_negative ) 
  cout << "Draw";
else if( monster_negative ) 
  cout << "Hero wins";
else 
  cout << "Monster wins. Great!";
monster_negative, what do you mean? I'm supposed to make it a variable?
that was pseudo code. Check if the monster hp is below 0.
Bazzy wrote:
In C++ they are standard operators ( not macros ) see [lex.operators] in the ISO document.


Has anyone tried overloading "operator and()"? Does it compile? If so, is it independent of operator &&()?
They should produce the same token.
This compiles and prints "ok" using g++ 4.5.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

struct S
{
    void operator and ( S )
    {
        std::cout << "ok" << std::endl;
    }
};

int main()
{
    S a,b;
    a && b;
}


(edit) should work as well in an implementation where and is a macro as long as that macro is defined and expands to &&
Last edited on
Interesting. For the macro version, I would expect the spaces before and after "and" in "operator and (" to be required.
BTW, using g++ this #define and && will give the following error: "and" cannot be used as a macro name as it is an operator in C++
1
2
3
4
5
if (mhr >= 0 && hhr >= 0)
	cout << "Draw!\n";
		else if (mhr >= 0)
			cout << heroname << " wins!";
		else cout << monster <<" wins!";


If both hero's and monster health is below 0 (if equal 0 it's okay) it says the monster won.
@Nuc

I think your arrows are pointing the wrong way, your code specifies the condition greater than or equal to. Do you mean less than or equal to?

<=
Last edited on
Pages: 12