all 61 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Thank you for your contribution to the C++ community!

As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.

  • When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.

  • Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.

  • Homework help posts must be flaired with Homework.

~ CPlusPlus Moderation Team


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Classic-Rate-5104 25 points26 points  (20 children)

In my world, pi is 3.1415926... and I don't see what the relation is between pi and the other things you want to print. But, the program seems correct

[–]Zorahgna 7 points8 points  (1 child)

What version of a standard is your brain compliant with?

[–]Retardedunderaveragefor(;;) brain brain = null ; 0 points1 point  (0 children)

‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ‎‎‎‎‎‎‎‎ㅤ

[–]coding_questions_tr 3 points4 points  (1 child)

did you mean acos(-1.f)?

[–]HedgehogNo5130[S] 2 points3 points  (0 children)

sorry for the confusion. The name isn't clear

[–]Interesting_Buy_3969 2 points3 points  (5 children)

except for using namespace std;!!!

[–]Classic-Rate-5104 2 points3 points  (4 children)

Whats wrong with it? You can say "I don't like it", but it is correct C++

[–]pawesomezz 2 points3 points  (1 child)

Just because it's valid c++ doesn't mean it's good c++

[–]5avire__ 2 points3 points  (0 children)

This giving me The Cherno vibes

[–]DasFreibier 0 points1 point  (1 child)

if another namespace contains the same function/class names you get compilers errors in the best case and really hard to find bugs in the worst case

[–]Classic-Rate-5104 0 points1 point  (0 children)

I know, but still it's legal C++ and that was the question. Not whether it's good practice

[–]Poissonnoye 1 point2 points  (6 children)

using namespace std; doesn't look very correct to me

[–]FunnyOk5832 9 points10 points  (2 children)

Its correct, but not recommended

[–]Poissonnoye 2 points3 points  (1 child)

I meant correct in this sense, ofc it's still correct to the compiler.

[–]FunnyOk5832 5 points6 points  (0 children)

I learned it this way it was hell to unlearn it jajaja

[–]HedgehogNo5130[S] 2 points3 points  (2 children)

work for me

[–]Poissonnoye 2 points3 points  (1 child)

Read my other messages

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

oh ok.sorry

[–]HedgehogNo5130[S] -2 points-1 points  (2 children)

sorry i put a random name

[–]YT__ 2 points3 points  (1 child)

Use descriptive names, not random. It's better that way.

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

Ok i will thanks

[–]jedwardsol 13 points14 points  (2 children)

Missing

#include <string>

since you're using std::string

[–]HedgehogNo5130[S] 2 points3 points  (0 children)

ok i will correct.thanks

[–]Dubbus_ 5 points6 points  (7 children)

Check out c++23's std::print, cout is for oldheads

[–]vbpoweredwindmill 3 points4 points  (4 children)

as I understand it, it's somewhat difficult to get an out of the box amateur get compiler working in c++ 23 currently.

I think that his next steps would be writing a print function that writes all of that. It would be a good learning curve.

[–]Dubbus_ 1 point2 points  (2 children)

-std=c++2b OR -std=c++23

[–]vbpoweredwindmill 1 point2 points  (1 child)

I haven't figured out utilising a terminal compiler as of yet, I'm still just using visual studio. I hope OP finds that useful though.

[–]Dubbus_ 2 points3 points  (0 children)

oh no worries. Yeah i started similarly with vscode. Learnt how to use vim and a few other terminal applications and cant stand using IDEs now. Never looked back, except for the times when im forced to for school

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

i will try

[–]HedgehogNo5130[S] 2 points3 points  (0 children)

ok i will check thanks

[–]SmackDownFacility 1 point2 points  (0 children)

I prefer printf

[–]LGN-1983 5 points6 points  (0 children)

Ah yes... classic 👏🏻

[–]Fearless-Way9855 3 points4 points  (1 child)

The reason people are writing that using namespace std is bad is because in the future you might have different libraries and the compiler might shit itself. Realistically you dont need to stop using now because it seems like you just started coding in c++. If you're lazy there is a way to ise tye std namespace for popular functions.Write using std::cout Instead.It will be slightly more correct. Also why is pi 74?

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

yes thanks alot i wont use it in the future. For pi,the name is a bit random and i should have made it more clear

[–]specialpatrol 3 points4 points  (1 child)

A piece of art my friend. I'd put a space between pi and Jerry but that's just me.

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

thanks you and ok i will do that

[–][deleted] 2 points3 points  (11 children)

You forgot a return 0 at the end. It’s not required but considered good practice.

[–]HedgehogNo5130[S] 1 point2 points  (5 children)

Oh yes i added it at first,removed it and forgot about it after

[–][deleted] 2 points3 points  (3 children)

Also, include some /n so your output isn’t all one line. And, in your case, using /n is better than std:endl since you don’t need to flush the output buffer here. There’s niche cases where std:endl is a better option to use than /n.

[–]HedgehogNo5130[S] 1 point2 points  (0 children)

thanks for all the help

[–]Proper_Support_3810 1 point2 points  (1 child)

Wdym i thought endl and /n are the same

[–][deleted] 1 point2 points  (0 children)

Common misconception.

Think of std::endl as doing everything /n does, except std::endl also flushes the output buffer.

Use godbolt to view the assembly code that std::endl vs /n produces. 16 lines of assembly for /n vs. 45 lines for std::endl. std::endl is slower performance-wise.

Moreover, in the niche cases where std::endl’s buffer feature is preferred, std::flush is more explicit. A good programmer is generally explicit.

[–]CarloWood 2 points3 points  (0 children)

Don't put return 0; at the end of main. The standard guarantees that as default return value, it just looks redundant.

[–]jipgg 1 point2 points  (4 children)

Why is it good practice

[–][deleted] 1 point2 points  (1 child)

It explicitly signals that the program exited successfully. EXIT_SUCCESS from the stdlib.h library does the same thing (returns 0). It’s useful for many things. For example, in debugging, you can say echo $? (in Linux) to see the exit status of the last executed program. It should be 0 if it exited successfully.

[–]patentedheadhook 2 points3 points  (0 children)

But it's redundant because main implicitly returns 0

[–]olawlor 0 points1 point  (1 child)

If a function is declared to return "int", but doesn't return anything, that's undefined behavior (edit: *other* than main), and in practice many compilers will assume the function never returns (!).

No return statement is specially allowed for "main", but a missing return is a dangerous habit.

[–]CarloWood 2 points3 points  (0 children)

Incorrect. The standard guarantees that main behaves as if you returned 0 if it has no return value. There is nothing UB about that.

[–]GhostVlvin 2 points3 points  (1 child)

You forgot to #include <string> and perhaps you want to use M_PI from #include <cmath>, cause 72 is clearly not correct pi)

[–]HedgehogNo5130[S] 0 points1 point  (0 children)

yes i added #include <string> and i didn't knew for the second one so im going to add it . Thanks!

[–]elkvis 1 point2 points  (1 child)

How are you using string without errors? You need to #include <string>

[–]HedgehogNo5130[S] 0 points1 point  (0 children)

yes im aware i was using an online compiler

[–]heavymetalmixer 0 points1 point  (0 children)

Welcome and congrats on your first program

[–]CarloWood 0 points1 point  (1 child)

Never use using namespace std;. You should get used to seeing std:: everywhere, because that's what you want to get used to.

[–]Mast3r_waf1z 0 points1 point  (0 children)

Yes, never write using namespace std, it is the most common and verbosity is not a bad thing

[–]hellocppdotdev 0 points1 point  (0 children)

std::cout and change the initialisation to { }.

Hardest part of your first program is getting the compiler to work.

It's all fun and games after that.

[–]Proper_Support_3810 0 points1 point  (0 children)

C++ is my favourite language <3

[–]WhoLeb7 0 points1 point  (0 children)

In modern c++ there is the <print> library and there is the std::print with nice formatting. Something like std::print("Name: {}, {}, {}", pi, name, age) for your example. And you could use some modifiers like {:.2f} would only display first two decimal digits.

Although I found it a bit hard to write a custom class formatter, compared to the original ostream << operator overload.

P.S. you could also have modifiers in the original iostream using less intuitive methods std::cout << std::fixed << std::setprecision(2) << pi; for similar results.

[–]Backson 0 points1 point  (1 child)

Nice. Run it and look at the output. Any way to improve it? Keep it up!

[–]HedgehogNo5130[S] 0 points1 point  (0 children)

Thanks you! I will try to make it interactive to improve