Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

cout and color

Posted on 2005-05-10
8
Medium Priority
?
1,204 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:Luxana
  • 3
  • 3
  • 2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13974376
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13977636
hi

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

which OS are u using Windows ?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13977741
Also have a look to this article:
http://www.codeproject.com/cpp/AddColorConsole.asp
0
Independent Software Vendors: 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!

 
LVL 10

Author Comment

by:Luxana
ID: 13983600
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
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13985185
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
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13985513
Everytime you change foreground or background color, it wil print all next text in that color.

Also you can perfect your function:

WORD ChangeColour(WORD color)
{
    static CONSOLE_SCREEN_BUFFER_INFO info;

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // Get handle to standard output
    GetConsoleScreenBufferInfo(hConsole, &info);   // Get current attributes
    SetConsoleTextAttribute(hConsole,color);  // set the text attribute of the previous handle
    return info.wAttributes;
}

So, you can save a previous color, you can use it as:

int main()
{
    WORD defaultColor;    // Will save default color

    defaultColor = ChangeColour(FOREGROUND_BLUE);      // Set the foreground colour to be blue, AND SAVE DEF COLOR
    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");
    ChangeColour(defaultColor);   //  <------------ return to default color
    return  0;
}

Good luck
0
 
LVL 10

Author Comment

by:Luxana
ID: 13992076
>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
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13994423
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

578 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