Solved

Cout color

Posted on 2004-08-29
9
9,145 Views
Last Modified: 2012-05-05
Is there any way to change the color of the cout statements, or cin for that matter?
0
Comment
Question by:teken894
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926723
Do you mean you want to display text in colors? If so, read this recent question:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21111434.html
0
 

Expert Comment

by:__init__
ID: 11929208
A simple and relatively portable way is using ansi escape sequences. On windows you might need to install ansi.sys driver

cout << "\033[31;43mHello world\n"; // red on yellow

http://www.evergreen.edu/biophysics/technotes/program/ansi_esc.htm
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 25 total points
ID: 11929806
Good answers have been given above, but for a more VC++ based solution, this is a fair example:

#include  <stdio.h>
#include  <windows.h>

void ChangeColour(WORD theColour)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // Get handle to standard output
    SetConsoleTextAttribute(hConsole,theColour);  // set the text attribute of the previous handle
}

int main()
{
    ChangeColour(FOREGROUND_BLUE);      // Set the foreground colour to be blue
    printf("This text is blue\n");      
    ChangeColour(FOREGROUND_RED);     // Set the foreground colour to be blue
    printf("This text is red\n");    
    ChangeColour( FOREGROUND_BLUE | BACKGROUND_RED);  // Set the foreground colour to be blue, the background to be red
    printf("This is blue text on a red background\n");  

    return  0;
}
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:teken894
ID: 11932081
jaime_olivares : As I am not running any unix based systems, I cannot do the ansi stuff...

__init__: also ansi.sys will make my program non-compatible with those with no ansi.sys, and im not thinking of porting, so why go throguh the trouble..

system("color //hex"); changes the overall color of the prompt.

also (FORE)(BACK)GROUND_INTENSITY make the colors brighter, exactly what I need..

anyone know how to make orange??, I tried to no success.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11932176
You can actually mix the colors, eg

ChangeColour( FOREGROUND_BLUE | FOREGROUND_RED); // Purple

but unfortunately FOREGROUND_YELLOW does not exist.

I dont think there is a way to make orange hence.
0
 

Author Comment

by:teken894
ID: 11932204
I made yello by mixing colors, i think it was red and green..

guess u can't use parenthesis for precedence in making orange
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11932325
Nope, tried but could not figure it.  You may be able to if you spend enough time playing about, but even with

WORD COLOR_YELLOW = (FOREGROUND_RED | FOREGROUND_GREEN);

    ChangeColour((FOREGROUND_RED) | COLOR_YELLOW);      // Set the foreground colour to be blue
    printf("This text is Orange (?)\n");  

the text still comes out just the same as COLOR_YELLOW...
0
 

Author Comment

by:teken894
ID: 11932341
Thanks anyway!
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11932361
No problem.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question