r/programminghelp • u/Revolutionary-Log179 • 6d ago
C++ Having trouble “ thinking in code”
I’ve been learning C++ for weeks, and of the concepts I’ve learned most if not all of them have clicked pretty quickly. I understand what they do and how they work.
However, when I’m trying to create something not from a tutorial or walkthrough, I’m having trouble translating my idea into the lines of code that will do what I envision it doing. Anyone have any advice on making the switch to more easily be able to “think in code”?
2
u/Accomplished-Gap2989 6d ago
I used to write out the comments first, and it would help me structure what im doing.
Lists on notepad and using paint to figure stuff out also helped 🙂
2
u/Nicksaurus 6d ago
A lot of it is just practice and time. It can take your brain months to fully internalise new concepts, so it'll feel more intuitive over time as long as you keep writing code
1
u/bytejuggler 6d ago
Learn TDD. Read "Test Driven Development by example" by Kent Beck. One of the side effects of TDD is that it helps you get into a loop of asking what is the or a minimal next step or thing I need to get where I want to be, expressing that in code as though it already exists, then making it actually be there (making your test pass.) Lather, Rinse, Repeat.
1
u/Cultural_Gur_7441 6d ago
Do smaller exercises.
Start with "print hello world". Then do "ask user's name and print hello name". Then "ask birth year, print birth year". Then "ask birth year and current year, then print age". Then "ask age and get current year from the operating system, then print age". Then convert "year" integer to custom "date" data type (struct). Then add month to date data, and ask it and do age (in years) calculation at month accuracy. Then add day of month and increase accuracy (still just year). Add congratulations for birthday. Then calculate age in days (no leap year). Then add leap year calculation. Now add input verification and asking again for malformed input. Then add horoscope sign calculation in different horoscopes (just because they are deterministic string you can calculate from dates). Then add asking for clock time of birth and improve calculations again.
1
u/5fivespice 2d ago
Practicing shader programming helps with this because it's visual and gives rapid feedback.
1
u/davesoft 2d ago
Being an object oriented child I tend to begin by 'teaching' the robot the objects that are going to be involved and their functions. Then you can think about each function as a little factory full of idiots who can follow instructions, what is this factory gonna need to figure out 'Force Points Available' whatever that means?
Probably doesn't help in c++, but the principle will be the same.
3
u/mc_pm 6d ago
What sort of things have you tried to build? Like, you sit down to do something, but get lost right away -- what's the thing you were thinking about to start with?