Link to home
Start Free TrialLog in
Avatar of Luxana
LuxanaFlag for Australia

asked on

cout and color

Hello experts;


Is is possible in this small program cout my text in color?

#include<iostream>

using namespace std;

int main() {

      cout << "This text has RED COLOR\n";

      return 0;
}

so this text will apear on screen in red?

thanks
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Avatar of furqanchandio
furqanchandio

hi

no cout can't print in color unless you call OS specific functions

which OS are u using Windows ?
Avatar of Luxana

ASKER

This is working but there is a problem with output

#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");  
      ChangeColour(FOREGROUND_GREEN);      // Set the foreground colour to be green
    printf("This text is blue\n");
    return  0;
}

This text is blue
This text is red
This is blue text on a red background
This text is green
Press any key to continue  <== this is green as well. I need this in standart colour otherwise everthing after this point will be green.

furqanchandio :
using g++ compiler as wel asi visual C++ ===> in this case prefered is visual c++

thanks for help
well sounds pretty simple


ChangeColour(FOREGROUND_GREEN);      // Set the foreground colour to be green
    printf("This text is blue\n");
 ChangeColour(FOREGROUND_WHITE);  // this ought to do it, check for the white color
 return  0;


cheers
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luxana

ASKER

>well sounds pretty simple
>ChangeColour(FOREGROUND_GREEN);      // Set the foreground colour to be green
>    printf("This text is blue\n");
>ChangeColour(FOREGROUND_WHITE);  // this ought to do it, check for the white color
> return  0;
>well sounds pretty simple

Well yes, and that was exactly first thing what I did. But it does not work. In fact I found that I'm able to use just RGB colors.

jaime_olivares:
your code is working without problems, great work and thanks for help.

./lubo
hi luxana

soory i tried to help you without getting my own hands dirty
but i hope my comments may have helped in you in way or another

cheers