Professional Documents
Culture Documents
Code
https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 1/5
12/7/2018 Center Text With Cout? - C And C++ | Dream.In.Code
12 } Graphic Design
351 More C++
Tutorials... Flash & ActionScript
walstib Blogging
Re: Center text with cout?
Posted 16 December 2005 - 11:57 AM Reference Sheets SEO & Advertising
Thanks for the replies and for the code snippet! I appreciate
it. Web Servers & Hosting
pcunderwear Site Check
Re: Center text with cout?
Posted 25 December 2008 - 10:25 PM
(https://www.dreamincode.net/forums/index.php?
s=afc06f7c5336e3e6e8709372283af9e9&app=forums&module=forums§ion=findpost&pid=136696)
born2c0de, on 16 Dec, 2005 - 05:55 AM, said:
walstib, on 16 Dec, 2005 - 10:52 AM, said:
Is it possible to get the text and the variable assigned to a
string, get the length of the string, subtract from 80 and
divide by 2 to pad left side with that many spaces?
Yes...that's how you do it.
It's simply putting that formula into use. Code Snippets
This Function is what you need. C Snippets
C++ Snippets
01 #include <iostream>
02 #include <string> Java Snippets
03
04 void centerstring(char* s) Visual Basic Snippets
05 {
C# Snippets
06 int l=strlen(s);
07 int pos=(int)((80-l)/2); VB.NET Snippets
08 for(int i=0;i<pos;i++)
09 cout<<" "; ASP.NET Snippets
10
PHP Snippets
11 cout<<s;
12 } Python Snippets
Ruby Snippets
i have created a more "accurate" way of doing this operation. ColdFusion Snippets
im a N00B at programming, but I've made some far out S#!t
with very little experience. SQL Snippets
The above code only works if the width of the screen is 80,
but my slight changes make text centering work at any DOS
width... Assembly Snippets
cout<<s; Flash/ActionScript
}
Snippets
this captures the screen width and manipulates the cout
function to correspond to the centering according to the ASP Snippets
current width...
Linux, Unix, and Bash
and also, im not sure if you need these or not, but i had
them: Snippets
#define _WIN32_WINNT 0x0500
#define SCREENWIDTH getmaxx() Other Languages
put those above your #includes. Snippets
YOUR WELCOME!
Regex
https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 2/5
12/7/2018 Center Text With Cout? - C And C++ | Dream.In.Code
don't use the variable of a string, just type something in
between the "s. Bye Bye Ads
This post has been edited by pcunderwear: 25 December 2008 -
10:28 PM
Hyper
Re: Center text with cout?
Posted 26 December 2008 - 03:26 AM Dream.In.Code
(https://www.dreamincode.net/forums/index.php? 89.047 Me gusta
s=afc06f7c5336e3e6e8709372283af9e9&app=forums&module=forums§ion=findpost&pid=136687)
walstib, on 15 Dec, 2005 - 08:25 PM, said:
Hello,
I am trying to find a way to center output on the screen. The
output will vary in length so I can not just pad with a set Me gusta esta página Registrarte
number of spaces.
Here is a very simple function you can use to center it: Sé el primero de tus amigos en indicar que te
01 void PlaceCursor(const int x, const int y) { gusta esto.
02
03 HANDLE hConsole =
GetStdHandle(STD_OUTPUT_HANDLE);
04
05 COORD PlaceCursorHere;
06 PlaceCursorHere.X = x;
07 PlaceCursorHere.Y = y;
08
09 SetConsoleCursorPosition(hConsole,
PlaceCursorHere);
10 return;
11 }
Of course, you could expand on that (after you get better and
move onto more advanced stuff with C/C++), and have it
check the size of the console screen (doing simple math, you
can find where the "center" is).
Here's an example of PlaceCursor used in conjunction with
your.. snippet:
1 PlaceCursor(25, 25);
2 cout << "Your login ID is " << loginID << endl;
3 cout << "Your password is " << password << endl;
4 cout << "The beginning balance on your account is
$" << balance << endl;
Hope this helps! If it did, or does help, please click the little
"This was a helpful post!" button, for my ego boost. :)
Strategy Master
Re: Center text with cout?
Posted 26 March 2009 - 07:31 PM
(https://www.dreamincode.net/forums/index.php?
s=afc06f7c5336e3e6e8709372283af9e9&app=forums&module=forums§ion=findpost&pid=136696)
born2c0de, on 16 Dec, 2005 - 05:55 AM, said:
walstib, on 16 Dec, 2005 - 10:52 AM, said:
Is it possible to get the text and the variable assigned to a
string, get the length of the string, subtract from 80 and
divide by 2 to pad left side with that many spaces?
Yes...that's how you do it.
It's simply putting that formula into use.
https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 3/5
12/7/2018 Center Text With Cout? - C And C++ | Dream.In.Code
This Function is what you need.
01 #include <iostream>
02 #include <string>
03
04 void centerstring(char* s)
05 {
06 int l=strlen(s);
07 int pos=(int)((80-l)/2);
08 for(int i=0;i<pos;i++)
09 cout<<" ";
10
11 cout<<s;
12 }
(https://www.dreamincode.net/forums/index.php?
s=afc06f7c5336e3e6e8709372283af9e9&app=forums&module=forums§ion=findpost&pid=498550)
pcunderwear, on 25 December 2008 - 09:25 PM, said:
void centerstring(char* s)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD NewSBSize;
NewSBSize = GetLargestConsoleWindowSize(hOut);
int l=strlen(s);
int pos=(int)((NewSBSize.X-l)/2);
for(int i=0;i<pos;i++)
cout<<" ";
cout<<s;
}
https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 4/5
12/7/2018 Center Text With Cout? - C And C++ | Dream.In.Code
https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 5/5