C++ problems

Started by
16 comments, last by Tome Steve Srsan 11 years, 9 months ago

I am just going to repeat what you have already been told, where you have your if (attack == "a weapon" , should be if (attack == "a weapon" || "weapon" || "the weapon").

That, too, will always resolve to 'true'.

Stephen M. Webb
Professional Free Software Developer

Advertisement

I am just going to repeat what you have already been told, where you have your if (attack == "a weapon" , should be if (attack == "a weapon" || "weapon" || "the weapon").
Also where you first create healthme and healthit declare the amount there.... int Healthme = 500;

You should really look into the different operator's and method's of calling loops happy.png


That will simply resolve to 'true' for every execution as his last form would. You are executing attack == "a weapon", which calls std::string's overloaded == operator, and returns true or false. However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".

The way that doesn't fail is:


if (attack == "a weapon" || attack == "weapon" || attack == "the weapon")


However, judging by the the set of strings, I wouldn't be surprised if one could get away with


if (attack.find("weapon") != std::string::npos)

However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".
[/quote]
Those are logical OR, not bitwise OR. The compiler determines that "weapon" is a non-null pointer, so you have [some boolean] || true, which evaluates to true, short circuiting the final condition.


However, judging by the the set of strings, I wouldn't be surprised if one could get away with

if (attack.find("weapon") != std::string::npos)

[/quote]
To nitpick, the user could type something ambiguous, like: "a magical weapon". In this case the program would behave differently if it checked for the presence of "weapon" or "magic" first.

I think your code behaves reasonably for this, but it is nice to be aware of such edge cases.
-Browser is being stupid, double post.-


However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".

Those are logical OR, not bitwise OR. The compiler determines that "weapon" is a non-null pointer, so you have [some boolean] || true, which evaluates to true, short circuiting the final condition.
[/quote]

Whoops. Looked like single-pipes on my screen.



However, judging by the the set of strings, I wouldn't be surprised if one could get away with

if (attack.find("weapon") != std::string::npos)


To nitpick, the user could type something ambiguous, like: "a magical weapon". In this case the program would behave differently if it checked for the presence of "weapon" or "magic" first.

I think your code behaves reasonably for this, but it is nice to be aware of such edge cases.
[/quote]

Aye, hence 'get away with'. I'm aware that there is the potential for edge cases, but he would need to handle those separately; there's no clarification in regards to the existence of said edge cases, so I didn't handle them :).
Never mind, I see what the problem is with the way I did that -.-;

Just now looked your code. Can you describe what this line is trying to do?


for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {


i know i got that wrong quite badly but all i'm trying to do there is set a for that looks at both of those conditions before going through with the loop.
Actually don't worry i figured it out i got the game to work biggrin.png i'm cheering, thanks for everyone's input and help smile.png

This topic is closed to new replies.

Advertisement