There will always be bugs. Learn to deal with them.
Don’t write all your code and test it. Write a bit, test a bit, write a bit more, test a bit more…
Use the debugger to examine the values of variables. The debugger tells you The Truth, that is, what the program actually does. Compare that with what you think it should be doing
Love you some debugger. It will save you.