Solved

Cout color

Posted on 2004-08-29
9
9,112 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
Comment Utility
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__
Comment Utility
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
Comment Utility
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
 

Author Comment

by:teken894
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks anyway!
0
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
No problem.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now