CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2017
    Posts
    1

    How to print colors in Xcode OS X

    Hi all. I am learning C++ with tutorials online, but all of them seem to be on Windows. I got to the point where they were showing how to print in colours but apparently you need the command: #include <Windows.h>. OS X doesn't have that command. What is an alternative way I can print in color using 'cout'?
    Thanks.

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: How to print colors in Xcode OS X

    Hi, AFAIK there's a nice and free library available for manipulating the console under *nix systems and it is called ncurses.

  3. #3
    Join Date
    Oct 2008
    Posts
    1,456

    Re: How to print colors in Xcode OS X

    firtsly, note that iostream ( and hence cout ) just deals with character based input output and knows knothing about how those character will be displayed or ultimately interpreted.
    In order to control character appareance you'll need a terminal API, like windows console functions or a full fledged terminal ui library like ncurses suggested by AvDav.

    That said, most unix terminals support ansi escape sequences, that include simple color support, so no library is needed to use them:

    Code:
    #include <iostream>
    
    #define ANSI_RED     "\x1b[31m"
    #define ANSI_GREEN   "\x1b[32m"
    #define ANSI_YELLOW  "\x1b[33m"
    #define ANSI_BLUE    "\x1b[34m"
    #define ANSI_MAGENTA "\x1b[35m"
    #define ANSI_CYAN    "\x1b[36m"
    #define ANSI_BLACK   "\x1b[0m"
    
    int main()
    {
      std::cout << ANSI_BLUE "this is blue" ANSI_BLUE << std::endl
                << ANSI_RED "this is red" ANSI_RED << std::endl
                << ANSI_BLACK "this is black" ANSI_BLACK << std::endl;
      
      // remember to reset color before exit ...
    }
    clearly, the above is not portable and you should not use it for anything serious ... ( unless you're targeting a specific terminal emulator )
    Last edited by superbonzo; May 23rd, 2017 at 10:46 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to print colors in Xcode OS X

    Most *nix consoles provide some form of terminal emulation - usually default to VT100 AFAIK- so most VT100 escape codes work. See http://www.termsys.demon.co.uk/vtansi.htm for further escape code details.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured