bug in my code

closed account (Dy7SLyTq)
so i know this is a lot of code, but dont let that deter you. i only include it all because i dont know where the bug is but i do have a general idea.

http://pastebin.com/cxdxHfQs
How do you know you have a bug? What is happening that you don't think should happen? You can't just put us in a dark room in which there may or may not be a cat and say "find the black cat".
@DTSCode

Can you use the debugger on your IDE? That is the normal way of finding runtime errors.

If you are not using an IDE, there are shell versions.

Last edited on
closed account (Dy7SLyTq)
@LB: i know i realized that when i was in the car. sorry.
@TheIdeasMan: no im using g++ with text edit. i never understood debuggers, i usually try to do it myself with print streams. what do u mean shell versions

ps. i actually solved it by just staring at the code.
By shell version, I mean there is gdb which works from the command line (shell). There is an article about basic use of it in the Articles section top left of this page.

There is also the gdb man page, which you can view from the shell by typing man gdb or you can find it online. There are heaps of other man pages on all the different unix commands available from the shell.

You could probably find lots of other tutorials about gdb online as well.

Hope all goes well.
closed account (Dy7SLyTq)
thank you. so because ive never used a debugger, could you tell me if it would have caught this?

i made a while loop with true as the condition, and then a bunch of code in the brackets for it. the catch to break out of it was in an if statement at the end. accidently, i put a semicolon after the while loop so it just looped for infinite. would it have caught the rouge semi-colon?
A debugger doesn't just tell you where the problem is, you have to use the tools it provides (breakpoints, stack traces, etc...) to deduce where the problem is.
i made a while loop with true as the condition, and then a bunch of code in the brackets for it. the catch to break out of it was in an if statement at the end. accidently, i put a semicolon after the while loop so it just looped for infinite. would it have caught the rouge semi-colon?


If you kept breaking periodically to see what code was being executed, it would become obvious fairly quickly that you weren't leaving the loop condition.
closed account (Dy7SLyTq)
i know and i tested that but the semi colon was really well blended in
That's why you write the {braces} before you even fill in the (parenthesis)
closed account (Dy7SLyTq)
no i did that. my code was like this:
while(true);
{
//code...
}

i didnt see the semicolon
closed account (S6k9GNh0)
I would learn to use GDB. It's confusing at first... but once you start, you'll tell yourself later you should have started earlier. Every professional programmer needs to learn to use a debugger.
I format my braces like this:

1
2
3
4
while(true) {
//code...
}


Hopefully if you put the opening brace on the same line as the while or if or for, then you won't be tempted to put a semicolon in between. As L B was saying do this first:

while() {}

Then go back and fill in the details.

The compiler won't warn about this this situation, because while loops with a null statement are sometimes valid code. It should with a for loop though.

Speaking of warnings, do you have them at the highest level? Under g++ I use:

-Wall -Wextra -pedantic


If I want to be particularly pedantic, I can use
-Werror
which promotes warnings to errors.

With your code, you need more functions. Main has 280 lines - that is way too much. Having more functions can make code easier to debug because you can use the step-over facility to to execute all of a function, as opposed to stepping into it, doing 1 line at a time.

IMO, your code could do with more comments to explain what is going on - this might help you identify what code should go in a function. If I was a maintenance programmer, I would have to stare at your code and try and figure out what is happening. More functions makes it easier for the maintenance programmer too. This is important, because one day you might be working in industry or on an open source project.

Can you change your printf statements to std::cout ?

And avoid having using namespace std; - just put std:: before each std thing.

this:

counter = counter + 1;

can be written:

counter++;

You could change all your for loops to look more like this:

for(counter = 0; counter < (boardSize / 2); counter++)

Just so you know, there are also operators like +=, -=, *= and /=, which work like this:

MyVar += 2; //same as MyVar = MyVar + 2;

With statements that come after an if or loop, I always use braces even if it is only 1 statement - this will save you one day when you add more code. Indentation can show the intent, but it can make errors like that hard to see.

Hope all goes well.
closed account (Dy7SLyTq)
first to the while thing: it was a one time thing. i was just wondering if it was the kind of thing a debugger would catch. second i dont use those error flags because i didnt know about them but i will from now on. thanks for that. third i will try to make more functions. fourth as for the comments i started doing that this morning for that reason. i got an ide though for ribbon folding. fifth, there is one printf which i am using to keep flush with the printw's (which is what i think you meant) which i have to use because im doing it in ncurses. sixth: i like using namespace std; over std:: because there is a chance that i could miss one and it looks ugly to me. of course if my co workers use that over dumping it globally i will gladly comply. i am aware of ++ and -- and += and -= etc and how they , but once again, these just look ugly to me. dont ask me why, but i dont like it. but i do appreciate u taking ur time to help me
i like using namespace std; over std:: because there is a chance that i could miss one and it looks ugly to me. of course if my co workers use that over dumping it globally i will gladly comply.


The other thing to do is this:
1
2
3
4
5
6
7
8
9
//#includes

using std::cin;
using std::cout;
using std::endl;

using std::string;
using std::vector;
//etc 


Then you can use cin, cout etc without qualification. I tend to do a mixture - I use std:: for things which don't appear very often like std::find or std::sort say

The problem with having using namespace std; is that it brings in the entire STL into the global namepspace, potentially causing naming conflicts. Did you know there is a std::distance ?

but once again, these just look ugly to me. dont ask me why, but i dont like it.


Sometimes you have to go with the flow - when learning a new spoken language or maths say, you shouldn't say "I don't like that, it's ugly".

Perception is very important, If you were to stick to your current ideas, and show code like that at a job interview, they might well think you don't know what you are doing. You may well not get the job.

There are technical reasons as well. From what I remember of asm (a long time ago, using DOS API) there was an instruction that incremented a number. So count++ would be 1 instruction. Assignment and incrementing would be more. Modern compilers would probably optimise that, but that means compilation will take longer, which is important when there is lots of code. Projects these days can be in the order of tens or hundreds of million lines of code.

The reason that operator like ++ and += were invented was because they are easier when there is a long expression. There are lots things like this in C / C++, you should get used to various notations.

first to the while thing: it was a one time thing. i was just wondering if it was the kind of thing a debugger would catch.


As cire said, you should see this in the debugger.

As naraku9333 said, the debugger just gives you tools, you have to deduce the error yourself.



closed account (Dy7SLyTq)
ok thanks for the reply. (and i mean this btw) after i fix main and comment my code ill do the using namepace std::etc and make = + etc to ++ and += etc. also, while i have not learned no where near everything, I am also not new. I had been using ++ and += and -= and %= for a while before i sat through and read it, which is when i found that i thought it ugly. i didnt like it because it didnt flow as nice as = data + data. but i didnt realize what you said about optimazation
Topic archived. No new replies allowed.