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

C++ Witch

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).

>> Therefore, to facility the text, you need to insert (15 / 2) = 7 spacesDon"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 Boardsbromheads.tv.com and also AIHorizon.com"s artificial Intelligence BoardsCommunity Boards
See more: How To Write The Most Interesting Fact I Ever Learned From Research Was
Popular pagesExactly 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
