Sree,
A few tips on debugging. Try the following

1)Break your code into smaller logical chunks. A flow chart definitely helps.

2)Set as many breakpoints and watches as you can. One breakpoint before and after each loop may be just what you need to avoid getting into the conditions you have listed.

3)Step in and out of code as necessary.

4)Do some research on segmentation faults. They are sometimes the toughest to crack.

5)Also read up on version control systems. They are immensely useful, particularly when you are working on large projects.

6)Using an IDE like MS Visual Studio and a version control tool like CVS, has helped me immensely in all the above areas.

7)If nothing works after quite an effort, take a break. Chill and get some coffee/water. There are some days I’ve just been so frustrated with bugs, only to come back an hour later to find the mistake glaring at me. YES, exhaustion increases bugs.

3 hours is too much to spend on a silly bug. Spend time on learning the above simple techniques. I assure you that programming will be much more fun and you’ll get your stuff done much faster.

See if you can learn LISP (its a list processing language). I have read a lot of positive comments on the programming approach. People say that it helps us think about programming in an entirely different way. Anyway, thats my next language to pick up once I’m out of here.

Cheers,
Buzz.