Solved

Cout color

Posted on 2004-08-29
9
9,151 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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