1

In html5, you can do make colored text by doing this:

.foo
{
    color: rgb(0,0,0);
}

I have recently started c++, and would like to display colored text to the user. I've Googled this and came up with stuff like this:

http://www.cplusplus.com/forum/beginner/5830/

Duoas' code snippet (shown below) looked interesting and I tried to run it on XCode.

#include <iostream>
#include <windows.h>

int main()
{
    const WORD colors[] =
        {
        0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
        0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
        };

    HANDLE hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    WORD index   = 0;

    // Remember how things were when we started
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hstdout, &csbi );

    // Tell the user how to stop
    SetConsoleTextAttribute( hstdout, 0xEC );
    std::cout << "Press any key to quit.\n";

    // Draw pretty colors until the user presses any key
    while (WaitForSingleObject( hstdin, 100 ) == WAIT_TIMEOUT)
    {
        SetConsoleTextAttribute( hstdout, colors[ index ] );
        std::cout << "\t\t\t\t Hello World \t\t\t\t" << std::endl;
        if (++index > sizeof(colors)/sizeof(colors[0]))
            index = 0;
    }
    FlushConsoleInputBuffer( hstdin );

    // Keep users happy
    SetConsoleTextAttribute( hstdout, csbi.wAttributes );
    return 0;
}

However, the error I get is

2:21: fatal error: windows.h: No such file or directory
compilation terminated.

So here's my question: Is there an alternative to windows.h in XCode? If not, how can you display colors to the console?

(note: I am trying to make my hello world program display hello world in another color, rather than black)

Any help is appreciated. Thanks!

EDIT: Ok, thanks to duskwuff's answer, I know windows.h can only be run on windows. I have a PC, but it's not running on the compiler I am currently using. What compiler can I use to run the code sample?

1 Answers1

0

The sample code you are trying to use is highly specific to the Windows console, and has no direct equivalent in macOS.

The Xcode console does not support colors.

If you are running your application in the macOS Terminal (not the Xcode console), you can use ncurses to display colors. Alternatively, you can use color code escapes directly.

Community
  • 1
  • 1
  • Thanks for your response. Now that I realize that the sample code can not be run on XCode, what compiler can I run it on? I have a PC, but it is not running on the compiler I am using, Ubuntu. – Lawrence Tran Dec 06 '16 at 03:28