Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ center command

Started by MrGuild Jan 13, 2012 at 3:35 AM 12 replies 19.2k views
Original Post
MrGuild
MrGuild
How can I center my text in C++ in the least amount of code without having to make a function?

Thanks for the help if possible!>
boogyman19946
boogyman19946
We're going to need a little more detail about the environment you're working in. Is it a console, a GUI, a text file?
Yo dawg, don't even trip.
MrGuild
MrGuild
Sorry yes it's a console app :S
MrGuild
MrGuild
What do you mean? Can't I just simply somehow put my text in the center of the console application? Even if I need to create a function thats alright.
boogyman19946
boogyman19946
Hmm, I wish I used the console for a little more than debugging XD Anyhow, the text in a console is usually monospaced (at least I haven't come by one that wasn't), which means each character is the exact size as the other ones. If you can figure out how many characters will fit into a line in the console, you can do:


// suppose totalChars is the total amount of characters per line in a console
// and str contains the string you want to output.
std::cout << std::string( (totalChars-str.size())/2, ' ' ) << str << std::endl;


Basically what happens is: you take the total chars you have, take away whatever space the string will occupy, and then divide it in half. This represents how many spaces you have to insert so that your string is centered. I also can't say that creating an std::string variable just to add blank spaces is a good idea.

Unfortunately, I'm not aware of any standard way to find out how many characters you can fit into the console. If I'm correct, the Windows console always has the same size (even if you maximize) but Linux consoles can be of variable size, and usually are.
Yo dawg, don't even trip.
MrGuild
MrGuild
I have:


int totalChars;
totalChars = 25;
const char* str;
str = "Testing";
std::cout << std::string( (totalChars-str.size())/2, ' ' ) << str << std::endl;
return 0;


but it requests 'size' int 'str' I was wondering what that even was?


std::cout << std::string( (totalChars-str.size())/2, ' ' ) << str << std::endl;
RulerOfNothing
RulerOfNothing
If you use std::string to hold your character strings, then you can call the size() member function on a string to find out how long it is.
fastcall22
fastcall22
Or, you can use std::setw and std::right:

std::string str = "Testing";
std::cout << std::setw( (screenWidth + str.length()) / 2) << std::right << str;
rip-off
rip-off

What do you mean? Can't I just simply somehow put my text in the center of the console application?
[/quote]
The point being made is that standard, portable C++ does not make any assumptions about the nature of the standard output streams. They may not be consoles. They could also be files, sockets, printers, IPC devices such as pipes, the bit bucket or something more esoteric again. Not all of these have the concept of a fixed width which you could use to calculate a "center" from.

For instance, you can make std::cout a file in your program (shown here as foo.exe), by running it from cmd.exe like so:

C:\path\to\executable> foo.exe > output.txt

You can make std::cin a file using < input.txt too. It is also possible to redirect std::cerr, using 2>.

Running the entire program non-interactively and logging all output to standard streams could be done like so:


C:\path\to\executable> foo.exe < input.txt > output.txt 2> error.txt


Just FYI, you can also merge stdout and stderr output by using the following:


C:\path\to\executable> foo.exe > output.txt 2>&1


It is also possible to set up pipes to other processes, and that other program might print the data, send it across the network or do any number of things with it.

These interesting tricks aside, the proper solution is to use a console/terminal library, like ncurses.

However, as a beginner, assuming a fixed width console isn't all that bad, and you would be right in ignoring the "proper solution" for the time being and concentrating on learning the core language skills you will need.
MrGuild
MrGuild
I've never seen that done before wow, interesting. I will try this out. I don't understand it but I will google some of this to find out. Thanks a bunch!
boogyman19946
boogyman19946


const char* str;
str = "Testing";
std::cout << std::string( (totalChars-str.size())/2, ' ' ) << str << std::endl;



I believe these are your trouble lines. const char* is a pointer to a primitive data type which has no member functions (aka methods), alas, there is no function named size() in str. You have to use std::string instead of const char*.

This should be about right (I took fastcall's solution as it seemed more elegant).

std::string str = "Test string.";
std::cout << std::setw ( (screenWidth - str.size()) / 2 ) << std::right << std::str;
Yo dawg, don't even trip.
JonBonazza
JonBonazza
If you are absolutely limiting yourself to the windows console, then you can just hard code the width. The Windows console is 80 characters wide and (i believe) 52 characters high by default (Though don't quote me on the last number. I am just going from memory when working with Assembly).
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
MrGuild
MrGuild
Yea I kinda only need this made up for the windows console nothing else, so thanks for the default numbers I was having trouble getting them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.