Stupid Programming Mistakes

I program in C++ and often I find myself spending hours trying to fix stupid mistakes I make.

I'd say I can write the code I need at a good pace, but when I compile it sometimes gives an error. And when I try to figure out how to fix it, it can literally take a few hours.

Should I be worried? I'm unsure of how long it's normal to spend trying to just debug code rather than write it. However, I do enjoy coding a lot and I'll code from morning to bed time. Once I start I can't stop.

Examples of my mistakes that took really long to fix:

I wrote "vectorName.empty()" thinking the empty() member function clears the whole vector of all the elements. But it's actually the clear() member function.

I didn't read my own prompts of what values to type in, and I kept typing in letter characters which caused an infinite loop. The prompt said to only write numbers. This took me so long to realize.

I forgot to write ".txt" at the end of opening a file. I was looking through my logic in code trying to figure out what I'm doing wrong. But, of course, took over an hour for me to scroll up and see that ".txt" was missing...

The docs. Read'em.

Yeah I do but often it's not obvious what I don't know.

What I meant to ask in my post is if this is a sign that programming may not be for me and therefore something I shouldn't spend my time pursuing. I obviously want it as a career, but I'm also aware that intelligence has a lot to do with being good at it.

>Should I be worried?
Only if it bothers you to have those mistakes.
I've noticed the main thing that separates people who are more serious about programming and do it for a living vs. people who say they want to learn but never get anywhere is how much tolerance you have for tracking down and resolving minor details like the ones you mentioned.
Normies generally have no patience at all for that sort of thing and will give up even with very simple computer use basics like filling out fields in a user interface at the very first instant anything happens that runs contrary to their expectations of what should happen. A programmer on the other hand will easily blow an entire day trying to figure out how to get past some obscure obstacle to accomplish a task they didn't really need to accomplish in the first place.
It's more a matter of whether you care about figuring out autistically nuanced systems details than it is a matter of how smart you are.

>I'd say I can write the code I need at a good pace, but when I compile it sometimes gives an error. And when I try to figure out how to fix it, it can literally take a few hours.
That is very, very normal. Debugging, even of embarrassingly stupid mistakes, generally takes *at least* as long as writing your program in the first place.

>Should I be worried?
No.

>I'm unsure of how long it's normal to spend trying to just debug code rather than write it.
Three hours of getting something to work that took one hour to write a first version of is pretty common.

A classic situation that has happened to every experienced programmer is hunting several hours for what turned out a one-character bug, that the next person you showed it to saw immediately.

>What I meant to ask in my post is if this is a sign that programming may not be for me and therefore something I shouldn't spend my time pursuing.
Certainly not. No worries.

try reading the documentation user

I bet you losers read instruction manuals when assembling home appliances too.

One thing I can say is that I will sit for hours until I figure out what is causing a bug. I won't give until I get it, and so far, I've gotten them every time. And I very much enjoy going through my code trying to solve a problem. It frustrated me in a very fun way.

>A programmer on the other hand will easily blow an entire day trying to figure out how to get past some obscure obstacle to accomplish a task they didn't really need to accomplish in the first place.
This really hit home when I read it because I enjoy balancing the efficiency and readability of my code. So, I end up doing things that won't necessarily change the what the program does, but makes code run faster and/or look neater. The only reason I stop improving it is because the assignment has a due date, and I gotta do work for other classes.

Well, I think I'm doing pretty well then. I noticed I don't often make mistakes in the logic part of my code - it's mainly stuff like missing brackets, semicolons, or some other stupid shit.
I also have a strict rule that I won't write any code that I don't understand. Thought if it's something like a function from a library then I don't think I need to understand anything more than what the function accomplishes. Those library functions are like thousands of lines long.

Thanks a lot for the responses, guys, It made me feel much better.

>I wrote "vectorName.empty()" thinking the empty() member function clears the whole vector of all the elements
read the documentation

>and I kept typing in letter characters which caused an infinite loop
it takes 2 lines of code to prevent that infinite loop.
google "infinite loop if i input a letter"

>But, of course, took over an hour for me to scroll up and see that ".txt" was missing...
that's why you're taught to spare 1 line of code to check if the file open was successful or not.

you can find that code in examples section commonly found in ....... yup, you guessed it... the documentation

Back in the day you'd have had to painstakingly leaf through thousand-page tomes. Your IDE shows you prototypes on mouse over, you hit F1 and get a full doc complete with examples and pitfalls to avoid. If that doesn't help you can just google it and wonderful sites like cplusplus.com or stack overflow will almost always have the answer. If they don't you can just ask it and some seasoned veteran will respond to you in under a day most of the time. And kids are still too lazy to look at the docs or use sound practices like error checking

>Should I be worried? I'm unsure of how long it's normal to spend trying to just debug code rather than write it. However, I do enjoy coding a lot and I'll code from morning to bed time. Once I start I can't stop.

Most of the work is always debugging. Learn how to use visual studio's debugger.

>I didn't read my own prompts of what values to type in, and I kept typing in letter characters which caused an infinite loop. The prompt said to only write numbers. This took me so long to realize.

Throw in basic error checking.

>I forgot to write ".txt" at the end of opening a file. I was looking through my logic in code trying to figure out what I'm doing wrong. But, of course, took over an hour for me to scroll up and see that ".txt" was missing...

Don't you read error messages?

I do. Stay mad, brainlet. You'll waste away your life 'debugging' every aspect of it.

>Stupid Programming Mistakes
>I program in C++
There's your first stupid mistake

do you do some previous design before going to the coding phase or are you just a hacker?
Do you write tests? You should. Also make code work for you, if you are using a file 'txt', put some kind of checking feature that adds it if you forgot. (e.g. you made a variable named filename with value 'data' instead of 'data.txt'. Your code should check if the extension is there and is correct and, if it's not, change it, and possibly raise a warning too if you are making the code make too many decisions and assumptions on its own)
Do you write down everytime you fix an error? (this proved important for me, as I can forget how I solved a minor problem in a few weeks, when it appears again) Make a "times I fucked up and how I solved it" compilation file. You will save TONS and I mean TONS of hours, specially if you are prone to repeating these same small mistakes again and again.

Planing and designing carefully is even more important in C++ where you can easily (and sometimes are led to) shooting yourself in the toe.
Also read the fucking docs as people is telling you, semantic errors are completely unprofessional.

As a side note, don't ever use reddit spacing again in Veeky Forums.

Fuck off you don't belong here.

>error checking
I never understood the point of doing that. If your process runs OK then it isn't necessary, and if it crashes then you'll find out when it crashes.

that's fine for experienced programmers
but novices like OP never check to make sure the file got successfully opened, so...

>and if it crashes then you'll find out when it crashes

Yeah but you'll have no clue where the problem is. Also, what if the program is a server you don't want to crash for each minor mistake.

good wisdom here

oh boy. You will never, ever get a professional software job with that attitude.

>You will never, ever get a professional software job with that attitude.
Wrong, been a salaried developer for eight years now and in a life safety industry too.

that's normal OP, you'll get better with time

Really depends on which errornyou are talking about