Formatting the appearance of a program

Is there a way to format the text in a basic console program? By formatting I mean like changing the colour of text or maybe underlining it?
If yes, how?
Google is your friend.
Colours:
http://www.cplusplus.com/forum/beginner/5830/

As far as I am aware if you are creating non OS-dependant code then I don't think there is a way to underline text in the basic console without the user of some GUI API.
Last edited on
Went though the post. Didn't understand any of the code given there. And it states nothing about underlining.
I really would appreciate an explanation regarding the code.
Well in that forum thread they present two methods of changing colours. The first method is a windows dependant

system("Color 1A");

method which changes the current colour of the console output to '1A'.

The other method is non-OS dependant code which is a bit more complicated. What it does is that it created an array of bit colour codes 'colours[]' of which it iterates through changing the colour for every "Hello World" output until the user press any key. He changes the colour by using this function:

SetConsoleTextAttribute( hstdout, colors[ index ] );

and the 'colours[]' array is:
1
2
3
4
5
const WORD colors[] =
		{
		0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
		0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
		};


To answer your lack on input about underlining in the post, please reread what my original response was:

As far as I am aware if you are creating non OS-dependant code then I don't think there is a way to underline text in the basic console without the use of some GUI API.
Last edited on
What I want to know about the first form of code, the system dependent one.Since I am only making programs for the sake of learning and don't intend to use it in any more than as a hobby or a pass-time, it shouldn't be any problem. I will have to understand a lot more about coding before I understand the other method.

So how can I decide the color of the text (that is, what colour does 1A mean, or if I want to use any other colour, how can I change it to the code format)?
@ GodPyro: The second method, the one provided by Duoas, IS OS dependent. The "SetConsoleTextAttribute(...)" function is part of the Win32 API not STL, if you look again his second line of sample code is to include windows.h.

Honestly I don't see the problem with using "system(...)" in this context. None of the arguments against it are applicable to this case since the "color" is a command and not a seperate program.

As for a cross platform method to underline characters, you would have to change the character map for that environment and have a compatible character map to begin with. I haven't seen anything to suggest that the current STL supports this. The third party libs fake it by using precompiler checks to test the environment and set global macros so the other parts know which functions to use, just open the header files and it's pretty easy to see that's what their doing.
Last edited on
@Computergeek01 whoops my bad. :P

To find out which colours you are using i.e. "1A" I did a quick search and couldn't find a resource online displaying a table of the colour codes. The quickest solution would be to fool around a bit in your code and see what the bit translations are.
No way to convert them yourselves?
And is there a way to underline text, or Emphasize it?
@ GodPyro: Must have been a darn quick search, Nah just kidding I have this resource bookmarked: http://ss64.com/nt/color.html
Save that site, it comes in handy when working with Windows outside of C++.

@ OP: Sorry when I said "Character Map" I should have said Font Set. But that almost doesn't matter since again I've seen nothing to suggest that STL supports doing this.
@Computergeek01, Nice linkage. I have bookmarked it for future use. :D Thanks.

@Nisheeth, can I ask why you want to know this? Is this in the scope of a prorgamming project?
@Nisheeth, can I ask why you want to know this? Is this in the scope of a programming project?

I was trying out what I had learnt so far by making a Quiz. I was using a couple of famous quotes in the questions which I wanted to highlight to make them look separate from the question. I wanted to use it there

And thanks a lot Computergeek01.

Now once again, is there a way to emphasize text or underline it apart from changing colour (bold, italics etc)?
Last edited on
We have answered this question time and time again in our posts:

I don't think there is a way to underline text in the basic console without the use of some GUI API.


I recommend using an API e.g. ncurses
http://en.wikipedia.org/wiki/Ncurses
Last edited on
@ OP: Now once again, NO there is not. You need to use a third party library. If you pick one that I know I may be able to help you from there. But I will not write the code for you.
Sorry. Didn't notice! its late night here and I was a bit sleepy! My bad.
Topic archived. No new replies allowed.