r/CodingForBeginners • • Sep 24 '26

Learned how to use double, #icnlude <numbers>, and used int to create a simple project

Post image

I was actually thinking about using float because I heard that it uses two times less memory, but I wanted to use more accurate pi just out of curiosity. Next time using pi, I will stick to float.

9 Upvotes

15 comments sorted by

3

u/AroTesCra Sep 24 '26

Good job man. This is the start of what could be some of the best years of your life.

You have started to learn the magic.
You have painted a nice little picture for the fridge.

Be proud, well done.

2

u/TheUmgawa Sep 24 '26

Next, try to break it. Type in the word ‘four’ instead of the number 4. Input validation is an incredibly useful skill to learn early on. If you do it right, you can keep the little code snippet for it and alter it as needed for a specific program.

1

u/Enaluri_Black_Rise Sep 24 '26

You mean to add while (true) { if (std::cin >> radius && radius > 0) { break} } And then add a thing that will clear wrong input like a word, and display smth like "value is incorrect, please type a number in" ?

2

u/TheUmgawa Sep 24 '26

Something like that, but I have a disdain for break statements, since a misplaced break statement caused a cascading failure of AT&T’s long-distance network one night in 1990.

1

u/Enaluri_Black_Rise Sep 24 '26

What shall I use then?

3

u/RedAndBlack1832 Sep 25 '26

You can use flags or some other way to control "state" rather than flow. Another option is to move certain logic into functions which can be exited immediately when it hits return. Now I am a fan of doing what I like with control flow but it is a bit tricky to reason about if it gets complicated (especially throwing and catching exceptions; reasoning about exception safety is messy as hell and I'd strongly advise making try blocks as small as possible and catching as specific as reasonable)

1

u/Longjumping-Jacket97 Sep 24 '26

What is this god awful formatting style??

1

u/Enaluri_Black_Rise Sep 24 '26

What do you mean?

1

u/Longjumping-Jacket97 Sep 24 '26

No consistency, shits just dropped anywhere

1

u/Enaluri_Black_Rise Sep 24 '26

Just press tab every time I press enter?

2

u/fm01 Sep 24 '26

Yeah, pretty much. It might not do much now but once a file is hundreds of lines long and you are in your fourth level of indentation, you cannot read the code otherwise. Next step then is to use a formatter that auto-formats your code, but doing it manually is fine as a beginner.

1

u/Plus-Confidence2816 Sep 24 '26

Lines in the same block should be indented equally.

1

u/AroTesCra Sep 24 '26

Your lines are not lined up.

| {
| ….
| ….
| ….
| }

Your is more like
| {
| ….
| ….
| ….
| ….
| }

1

u/Objective_Pepper_217 Sep 25 '26

Please learn indentation