Center a cout or Funktion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Center a cout or Funktion

hey guys, how can you centre a cout in the console in C++.

7th Jun 2018, 2:04 PM
Talha Hameed
Talha Hameed - avatar
3 Answers
+ 6
There is no specific way to do so, since the console isn't specified as part of the C++ standard. It will be dependant on the console of your OS (and its version). What you can do, is to use whitespaces, or utilize methods within the iomanip header to achieve your desired output. Remember that console programming stresses on algorithms. If you want nice graphics, that's GUI programming stuff.
7th Jun 2018, 2:09 PM
Fermi
Fermi - avatar
+ 3
add a couple of /t eg. cout << "\t\t\t\t\t\t Center"<< endl;
7th Jun 2018, 2:11 PM
‎ ‏‏‎Anonymous Guy
+ 1
Try this : string center(string data) { if(data.size()%2!=0) data+=" "; size_t size=80-data.size(); // Assuming a default terminal // size of 80x25. stringstream ss; for(int i=0;i<size/2;i++) ss<<" "; ss<<data; for(int i=0;i<size/2;i++) ss<<" "; return ss.str(); } Now, you can do : cout<<center("Heading");
8th Jun 2018, 2:57 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar