We all make mistakes, that forms part of our experience. You learn from them to hopefully make sure you don't make the same mistakes twice.
I've make mistakes in my own programming silly things I really shouldn't done but still ended up doing it anyway. I'd probably be here ages if I tried to remember and write down all the silly mistakes I've made in some of my assignments.
As for advice:
1. If you really get stuck on a problem go away from it for a while and come back to it. Sometimes I find I need a break before I can go back to trying to solve a problem. I've come back sometimes and spotted the silly mistakes that I've made which I just couldn't see earlier.
2. I wouldn't recommend this for everyone but what I would do occasionally is write sections of my program onto paper and carefully go through and ensure the program is doing what it is supposed to do in your head.
Mind you recursion always confused me no end when doing this. Don't get me started on Scheme or Z. I found both of those to be incredibly frustrating.
Anyway sometimes you'll catch things when you go to write your program code to paper (for me these were more often or not the silly mistakes I tended to make).
3. As someone said earlier test, test, test, test. You can never do enough testing in my opinion.
4. Never leave anything to the last minute ( I learnt this from my 'A' level Computing project)...
I hope that helps.