LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2004, 12:32 PM   #1
Brakki
Member
 
Registered: Jun 2003
Distribution: Fedora Core 1
Posts: 33

Rep: Reputation: 15
Changing text color in C++


I've got a C program that demonstrates changing text color with ANSI escape codes. It works like this:
Code:
#include <stdio.h>
int main()
{
   int i = 0;
   for (i = 31; i <= 37; i++)
   {
       printf("\033[0;%dmHello!\033[0m\n", i);
       printf("\033[1;%dmHello!\033[0m\n", i);
    }
}
I'm wondering how to do something like this in C++, using cout. Anybody know?
 
Old 04-21-2004, 12:55 PM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
#include <iostream>

int main( void )
{
for (int i = 31; i <= 37; i++)
{
std::cout << "\033[0;" << i << "mHello!\033[0m" << std::endl;
std::cout << "\033[1;" << i << "mHello!\033[0m" << std::endl;
}
return 0;
}
 
Old 04-21-2004, 01:33 PM   #3
Brakki
Member
 
Registered: Jun 2003
Distribution: Fedora Core 1
Posts: 33

Original Poster
Rep: Reputation: 15
Awesome. Thanks.

Last edited by Brakki; 04-21-2004 at 02:13 PM.
 
Old 04-21-2004, 04:24 PM   #4
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 418

Rep: Reputation: 31
If you are writing software that will be used by others, and you need color text support, I'd suggest using ncurses to change the text's color. ASCII escape codes may not work on certain terminals.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VI Background Text Color mahugl Linux - Newbie 3 12-15-2013 10:12 PM
How change text color using linux in text mode only runlevel 3? Xavius Linux - General 7 05-07-2009 02:19 AM
Kweather Text Color aerogate Mandriva 2 11-19-2005 03:17 PM
How to get a color text console? dejavu_01 Debian 1 08-30-2005 11:38 AM
text color in the CLI hrc Linux - Software 7 06-25-2003 09:35 AM


All times are GMT -5. The time now is 10:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration