8 Replies - 84926 Views - Last Post: 18 December 2010 - 12:26 PM Rate Topic: -----

#1 walstib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 05

Center text with cout?

Post icon  Posted 15 December 2005 - 09:25 PM

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 number of spaces. My output looks like this:
cout << fixed;
cout.precision(2);
cout << "Your login ID is " << loginID << endl;
cout << "Your password is " << password << endl;
cout << "The beginning balance on your account is $" << balance << endl;




The loginID, password and balance variables can very in length and I would really like to have this output centered in the console. Any suggestions? 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? Is getting the text and variable into a string called casting? I have not had any luck with this so far.

I'm new to C++, please be kind... Thanks for any input!

Is This A Good Question/Topic? 0
  • +

Replies To: Center text with cout?

#2 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Center text with cout?

Posted 15 December 2005 - 09:28 PM

you can always make a functions that counts how many characters there are in the string and inserting the right amount of spaces before it depending on the lenght of the string.
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Center text with cout?

Posted 16 December 2005 - 06:55 AM

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.

This Function is what you need.

#include <iostream>
#include <string>

void centerstring(char* s)
{
   int l=strlen(s);
   int pos=(int)((80-l)/2);
   for(int i=0;i<pos;i++)
    cout<<" ";

   cout<<s;
}


Was This Post Helpful? 1

#4 walstib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 05

Re: Center text with cout?

Posted 16 December 2005 - 11:57 AM

Thanks for the replies and for the code snippet! I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#5 pcunderwear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-December 08

Re: Center text with cout?

Posted 25 December 2008 - 10:25 PM

View Postborn2c0de, 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.

This Function is what you need.

#include <iostream>
#include <string>

void centerstring(char* s)
{
   int l=strlen(s);
   int pos=(int)((80-l)/2);
   for(int i=0;i<pos;i++)
    cout<<" ";

   cout<<s;
}



i have created a more "accurate" way of doing this operation. im a N00B at programming, but I've made some far out S#!t with very little experience.
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...

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;
}

this captures the screen width and manipulates the cout function to correspond to the centering according to the current width...

and also, im not sure if you need these or not, but i had them:
#define _WIN32_WINNT 0x0500
#define SCREENWIDTH getmaxx()

put those above your #includes.


YOUR WELCOME!

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:
centerstring("YOUR STRING HERE");

don't use the variable of a string, just type something in between the "s.

This post has been edited by pcunderwear: 25 December 2008 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Center text with cout?

Posted 26 December 2008 - 03:26 AM

View Postwalstib, 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 number of spaces.


Here is a very simple function you can use to center it:
void PlaceCursor(const int x, const int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD PlaceCursorHere;
    PlaceCursorHere.X = x;
    PlaceCursorHere.Y = y;

    SetConsoleCursorPosition(hConsole, PlaceCursorHere);
    return;
}


An example of it would be:
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:

PlaceCursor(25, 25);
cout << "Your login ID is " << loginID << endl;
cout << "Your password is " << password << endl;
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. :)
Was This Post Helpful? 1

#7 Strategy Master  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-March 09

Re: Center text with cout?

Posted 26 March 2009 - 07:31 PM

View Postborn2c0de, 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.

This Function is what you need.

#include <iostream>
#include <string>

void centerstring(char* s)
{
   int l=strlen(s);
   int pos=(int)((80-l)/2);
   for(int i=0;i<pos;i++)
    cout<<" ";

   cout<<s;
}



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.
Was This Post Helpful? 1

#8 Guest_trusktr*


Reputation:

Re: Center text with cout?

Posted 06 July 2010 - 06:03 PM

View Postpcunderwear, 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?
Was This Post Helpful? 0

#9 Guest_tyler*


Reputation:

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.

Was This Post Helpful? 0

Page 1 of 1