Solved

Cout color

Posted on 2004-08-29
9
9,162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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: 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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 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