The as whole width the the heat is 20 characters. How have the right to I center a wire using print so thattitle becomesspace-title-spacei have something like this for this reason far

You are watching: How to center text in c++


C++ Witch
Join day Oct 2003Location SingaporePosts 28,334
Do a basic calculation. Because that example, "title" has actually a length of 5, so there should be 15 spaces. Therefore, to center the text, you must insert (15 / 2) = 7 spaces (or 8, if you choose to ring up).
I obtain maybe two dozen request for help with some type of programming or style problem every day. Most have more sense than to send me hundreds of lines that code. If castle do, ns ask lock to uncover the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest regime that demonstrates the error" is a an effective debugging tool.

>> Therefore, to facility the text, you need to insert (15 / 2) = 7 spaces
Don"t forget the the field-width needs to incorporate the size of the cable - due to the fact that it"s in the field.
#include #include #include #ifdef _WIN32#include int GetColumnWidth()//GetColumnWidth#elseint GetColumnWidth() return 80;#endifint main() const char *s = "Hey, I"m centered!"; const int total_width = GetColumnWidth(); const int s_width = strlen(s); const int field_width = (total_width - s_width) / 2 + s_width; printf("%*s\n", field_width, s); return 0;//main
Quick NavigationC ProgrammingTopSite AreasForumsGeneral Programming BoardsPlatform details and also"s artificial Intelligence BoardsCommunity Boards

See more: How To Write The Most Interesting Fact I Ever Learned From Research Was

Popular pages
Exactly how to acquire started through C++ (or C) today C TutorialC++ Tutorial5 ways you deserve to learn come program much faster The 5 Most typical Problems brand-new Programmers FaceHow to collection up a compiler8 typical programming mistake What is C++11?Creating a game, from start to finishRecent additions