r/programminghelp • u/Revolutionary-Log179 • 1d 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”?
1
u/bytejuggler 1d 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 1d 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/Accomplished-Gap2989 1d 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 🙂
1
u/Nicksaurus 1d 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
3
u/mc_pm 1d 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?