You are on page 1of 5

12/7/2018 Center Text With Cout? - C And C++ | Dream.In.

Code

Dream.In.Code> Programming Help> C and C++C++ Console


App
Page 1 of 1
8 Replies - 168095 Views - Last Post: 18
December 2010 - 12:26 PM Rate Topic:
walstib
Posted 15 December 2005 - 09:25 PM
Hello, Follow & Share General Discussion

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
Dream.In.Code Caffeine Lounge
number of spaces. My output looks like this: Corner Cubicle
Follow
1 cout << fixed; Student Campus
2 cout.precision(2);
Software Development
3 cout << "Your login ID is " << loginID << endl;
4 cout << "Your password is " << password << endl; Industry News
5 cout << "The beginning balance on your account is
Introduce Yourself
$" << balance << endl;
Nightmare.In.Code

The loginID, password and balance variables can very in


C++ Tutorials
length and I would really like to have this output centered in New Scanner Tutorial Programming Help
the console. Any suggestions? Is it possible to get the text
and the variable assigned to a string, get the length of the for Windows using WIA.
string, subtract from 80 and divide by 2 to pad left side with C and C++
that many spaces? Is getting the text and variable into a Microsoft : Shaped
string called casting? I have not had any luck with this so far. VB.NET
Windows
I'm new to C++, please be kind... Thanks for any input! Java
Microsoft :
Replies To: Center text with cout? C#
Implementing an
Mrafcho001 Python
Re: Center text with cout? Indexed Table : Part III
Posted 15 December 2005 - 09:28 PM PHP
you can always make a functions that counts how many Microsoft :
characters there are in the string and inserting the right Mobile Development
amount of spaces before it depending on the lenght of the Implementing an
string. ASP.NET
Indexed Table : Part I
born2c0de and II .NET Framework
Re: Center text with cout?
Posted 16 December 2005 - 06:55 AM
Processing Data Held In Ruby
walstib, on 16 Dec, 2005 - 10:52 AM, said:
Is it possible to get the text and the variable assigned to a A Comma Separated File Game Development
string, get the length of the string, subtract from 80 and
divide by 2 to pad left side with that many spaces? Assembly
The reasons for using
Yes...that's how you do it. vectors Databases
It's simply putting that formula into use.
This Function is what you need. Introduction to C++ ColdFusion
Metaprogramming: VB6
01 #include <iostream> Basics Other Languages
02 #include <string>
03 Pointers, and a possible 52 Weeks Of Code
04 void centerstring(char* s) problem - if you're not
05 {
careful!
06 int l=strlen(s); Web Development
07 int pos=(int)((80-l)/2); Generating Random
08 for(int i=0;i<pos;i++) Web Development
Numbers - The C++ Way
09 cout<<" ";
HTML & CSS
10 Hello World: Your first
11 cout<<s; JavaScript
C and C++ Programs

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&section=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

void centerstring(char* s) Functional


{
HANDLE hOut; Programming Snippets
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD NewSBSize; Perl Snippets
NewSBSize = GetLargestConsoleWindowSize(hOut);
int l=strlen(s); HTML/CSS Snippets
int pos=(int)((NewSBSize.X-l)/2);
for(int i=0;i<pos;i++)
cout<<" "; Javascript 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

oh, yeah, i forgot, if you want to use something, like a string


to center, type this wherever you want the string centered in
your program:
DIC Chatroom
centerstring("YOUR STRING HERE"); Join our IRC Chat

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&section=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 }

An example of it would be:

1 PlaceCursor(25, 25); printf("Hello World! I'm


centered");

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&section=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 }

How would you center a variable that contains a string value


with this code. This only works if you type in the string as a
constant.
Guest_trusktr*
Re: Center text with cout?
Posted 06 July 2010 - 06:03 PM

(https://www.dreamincode.net/forums/index.php?
s=afc06f7c5336e3e6e8709372283af9e9&app=forums&module=forums&section=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;
}

Hey, what exactly do i need in my header to make "HANDLE"


work and all the other stuff?
Guest_tyler*
Re: Center text with cout?
Posted 18 December 2010 - 12:26 PM
instead of the for loop which is quite harsh on the processor,
one could always just
int pos = 40 + int(l/2); cout << setw(pos);

any comments just email *****


This post has been edited by JackOfAllTrades: 18 December 2010 -
03:28 PM
Reason for edit:: Email address removed: This is not how a forum
works.
Page 1 of 1

Related C++ Topicsbeta


Centered Text

Error With Cout - Works If Within Brackets. Not


Outside

Character Array Intialization

Advanced File Operations On Inventory Program

#define Macro Problems - Wont Work With Cout<<

How To Search For Words In Text File?

How To Make A Text File

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

Updatable Text Output

C++ Loop Help - Issues With Do While And While


Loops

Hooking ExtTextOutW Not Gives The Text With Proper


Information

FAQ | Team Blog | Feedback/Support | Advertising | Terms of Use | Privacy Policy |


About Us

Copyright 2001-2018 MediaGroup1 LLC, All Rights Reserved


A MediaGroup1 LLC Production - Version 6.0.2.1.36
Server: secure3

https://www.dreamincode.net/forums/topic/13749-center-text-with-cout/ 5/5

You might also like