+ 1

How i can change only one line of output color? C++

i want too put the program with diferents colors, ex: hello(red). goodbye(black) anda the background white

23rd Mar 2018, 12:21 PM
Jorge Matos
Jorge Matos - avatar
2 Answers
+ 3
On windows you can use SetConsoleTextAttribute from the windows header. https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute And the attributes: https://docs.microsoft.com/en-us/windows/console/console-screen-buffers#span-idwin32characterattributesspanspan-idwin32characterattributesspancharacter-attributes So for example to do what you asked you would do something like this: #include <iostream> #include <windows.h> // c = 7 for default color void setConsoleColor(WORD c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); } int main() { setConsoleColor(FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED ); std::cout << "Hello\n"; setConsoleColor( BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED ); std::cout << "Goodbye\n"; setConsoleColor(7); } Use the | operator to combine attributes. And just like Manual said, this doesn't work on SL.
23rd Mar 2018, 1:07 PM
Dennis
Dennis - avatar
+ 3
You need to find what c++ code is used to change color for your os. search c++ change text color for what you run code on. - does not work on SL.
23rd Mar 2018, 12:31 PM
Manual
Manual - avatar