Change color of cout text C++
Asked Answered
G

2

0

Im trying to change the color of some of my cout outputs but after that cout to be again the same color.

Ive tried the following code:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
        cout << "Help-Please" << endl;

but it continues even after the cout has been completed. Also to add that some of these color changes will be inside if statements. What code would you recommend me to add to to get this effect?

Gomulka answered 10/11, 2011 at 22:44 Comment(0)
D
2

The text color stays in effect, you must call SetConsoleTextAttribute again with other (the previous) colors.

Edit: Each text output will use the colors of the last call to SetConsoleTextAttribute, so you can use different colors in different lines (or words/characters).

Dud answered 10/11, 2011 at 22:50 Comment(3)
I tried it thanks but it still keeps the highlight of the text how can I remove the highlight??Gomulka
What do you mean with highlight of the text? Did you remember to or FOREGROUND_... and BACKGROUND_... as appropriate?Dud
SetConsoleTextAttribute only affects newly-printed text. It does not go back and change the color of text already printed. If you want text already printed to change color, you need to go back and reprint them in the new color.Ostrom
H
-1

in the stdlib.h there is a color command that changes all the the text colors. Ie system("color 0B");

Hulahula answered 11/7, 2016 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.