C++ center command

Started by
12 comments, last by MrGuild 12 years, 3 months ago
Or, you can use std::setw and std::right:

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

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.
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!


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.

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
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.

This topic is closed to new replies.

Advertisement