+ 1
Center a cout or Funktion
hey guys, how can you centre a cout in the console in C++.
3 Antworten
+ 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.
+ 3
add a couple of /t
eg. cout << "\t\t\t\t\t\t Center"<< endl;
+ 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");