News:

11 March 2016 - Forum Rules

Main Menu

Need help with my code

Started by InfamousKnight, January 19, 2013, 10:43:10 AM

Previous topic - Next topic

InfamousKnight

I'm not going to post the entire code just the battle function:
http://pastebin.com/LVPtwSZW

For some reason, it totally ignores the conditions. I used recursion to make an infinite loop.

Also, notice in the for loop at the beginning, where does the curly bracket end? I can't tell..

Yeah, this game is going to become big someday.

furrykef

Rule #1 of asking for coding help: if you're not going to make your code readable, nobody will help you with it.

Please indent your code using only four spaces per indent. Maybe eight if you want to get really old-school, but that's the absolute maximum. Also, don't indent things for no reason. Why are lines 6 and 12 indented? Why are you indenting the while statement on line 23 itself and not just its body? Why is the for statement on line 49 not indented? This isn't even an exhaustive list. I indented my code properly in C and C++ from day one. Why can't you?

InfamousKnight

#2
Mainly because I'm not used to it. I usually just press tab a couple of times. I'll try and indent it now.

I have no clue why those random indents are there. It doesn't do that in my original code.. I guess when I copied and pasted it it came out all jacked up. Sorry, Idk how I'm supposed to fix that unless I reindent it in the pastebin which I don't feel like doing. Meaning, the code on my machine is very nicely indented but not in that pastebin for some reason.

You didn't think I would just randomly press tab did you?

Bregalad

I agree with what furrykef said, but you should try to use parenthesis in the constions in your if causes :
if ((Players.hp <= 0) && (Players.ally = "ally") && !players.dead)

I'm not saying this was your problem, but it could have been it. Some compilers can be bitchy about this, as technically & (and therefore &&) can be interpreted both as a logical and or as a bitwise and.

furrykef

QuoteI have no clue why those random indents are there. It doesn't do that in my original code..
Perhaps check if you're mixing tabs and spaces for indents in your code. That's never a good idea. Seems doubtful that this is the problem, but might as well check anyway.

QuoteYou didn't think I would just randomly press tab did you?
With some code I've seen out there, one never knows.

Quote from: Bregalad on January 19, 2013, 11:34:55 AMI agree with what furrykef said, but you should try to use parenthesis in the constions in your if causes :
if ((Players.hp <= 0) && (Players.ally = "ally") && !players.dead)
You only need to do that when mixing && and || because they have different precedence levels. Otherwise, adding the parens won't hurt (and many programmers will do it), but it's unnecessary. And surely you mean '==', not '='.

QuoteI'm not saying this was your problem, but it could have been it. Some compilers can be bitchy about this, as technically & (and therefore &&) can be interpreted both as a logical and or as a bitwise and.
Erm, I'm sorry, but this is complete nonsense. & is always bitwise. && is always logical. Neither can ever be the other (unless you're doing something funky with operator overloading, but only a completely insane person would do this).

henke37

You are comparing char * with string literals. This is undefined behavior and will not likely not work at all unless the pointer was initialized with the same literal. Use an enum instead. Or even better, restructure your code so that you don't need to check the value, just use it directly.

Also, have you heard of a debugger? Try one. I recommend visual studio, but gdb will do just fine on posix.

furrykef

Actually I don't think it's "undefined behavior" in the ISO/ANSI sense. It just doesn't mean what the programmer probably thinks it means (and obviously not what InfamousKnight thought it meant).

Revenant

Quote from: InfamousKnight on January 19, 2013, 10:43:10 AM
I used recursion to make an infinite loop.

For the love of God, don't do this. This is what while loops are for.

Gideon Zhi

Quote from: InfamousKnight on January 19, 2013, 10:43:10 AM
I used recursion to make an infinite loop.

Quote from: Revenant on January 19, 2013, 10:21:21 PM
For the love of God, don't do this. This is what while loops are for.

No no no, do keep doing this! This sort of programming practice is what legends are made of.

Pikachumanson

Quote from: Gideon Zhi on January 19, 2013, 10:36:31 PM
No no no, do keep doing this! This sort of programming practice is what legends are made of.

Not sure if serious.

Revenant

Shit, have I been unnecessarily prescriptive?

In that case, carry on  :D

furrykef

#11
Oh wow, I totally missed that bit. Recursion to make an infinite loop? What the fuck?!

Normmatt

for(int i = 0; i <= 2; i++) {
     if(players.dead)i++;


Its always a bad idea to manually increment the for loop from within. Change that condition to check if the player is not dead and enclose the rest of the code in the loop in it.

furrykef

#13
Two things wrong with using recursion this way.

1. Don't use recursion except when it really is the best way to model the problem. This is almost never. In my experience, the only time I've needed recursion was when navigating a tree data structure. Even the textbook examples like factorials and fibonacci numbers shouldn't really be implemented with recursion; they're just handy for demonstrating how recursion works.

2. You can't recurse infinitely. Every time you enter a function, data is pushed onto your program's stack, and every time you leave a function, data is popped off the stack. This basically means that recursion eats up stack space -- the deeper you recurse, the more space you use -- until you return out of the recursive function. It's pretty easy to run out of stack space this way, at which point your program will crash.

Quote from: Normmatt on January 20, 2013, 12:22:17 AM
for(int i = 0; i <= 2; i++) {
     if(players.dead)i++;


Its always a bad idea to manually increment the for loop from within. Change that condition to check if the player is not dead and enclose the rest of the code in the loop in it.

I disagree that it's necessarily a bad idea to increment the counter, but in this case it does indeed make little sense. At the very least it could have been written,
if(players[ i ].dead) break;
(I'm only adding spaces around the "i" so the stupid forum won't think it's BBCode). In fact, without the break, I don't understand how this code is supposed to work.

And by the way, you usually shouldn't use <= in a for loop. It confuses people.

Kiyoshi Aman

I looked at the code. Nasir would totally autograph a print copy.

Rhys

This topic again? Seriously? >_>

Pikachumanson

Quote from: Rhys on January 20, 2013, 03:23:20 AM
This topic again? Seriously? >_>

Complete with text based battle code.

henke37

Quote from: Pikachumanson on January 19, 2013, 11:06:11 PM
Not sure if serious.
He is serious alright. It's the kind of legend that you tell to your grand children as a precautionary example. The kind of legend that they wont believe and you don't want to believe either.