can someone teach me or show me how to code a program that can output color text?? thx :p

This is an implementation-dependent operation. Without knowing your platform and compiler, we can't help you at all.

i am using dev c++ 4.9.9 compiler and windows xp

below is a simple source code

#include<iostream>
using namespace std;

int main()
{
cout<<"Hello"<<endl;
system("pause");
return 0;
}

how to let the program print the "hello" string with color and also how to set the background color??

Search MSDN for SetConsoleTextAttribute.

still can't understand can u show me some example source code

>still can't understand can u show me some example source code
No, why don't you try experimenting instead of mooching off everyone else. This isn't a terribly difficult task, and by working it out on your own you'll learn much more than if I just gave you code to copy.

i really dun understand all the things in MSDN

Looks like Narue is in the usual good mood this morning.
You have to forgive! Good person though!

I took BCX, the basic to C translator, and figured it out in 5 minutes flat. Here is the C code. You may throw in a little C++ lingo. The name of the WinApi header may be different with your compiler. Play with k a little to get the feel!

// change text color in Windoze console mode
// colors are 0=black 1=blue 2=green and so on to 15=white  
// colorattribute = foreground + background * 16
// to get red text on yellow use 4+14*16
// tested with Pelles C  (vegaseat)  

#include <stdio.h>
#include <windows.h>   // or other WinApi header


int main()
{
  HANDLE  hConsole;
	int k;
	
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  for(k = 1; k < 25; k++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%2d  %s\n", k, "I want to be nice today!");
  }

  getchar();  // cheap wait
  return 0;
}

Actually, programming can be fun ...
a man, sorry, person over machine thing!

thx for ur help vegaseat.

Do u know how to let the text in the program blink???

Do you now see what happens vegaseat? If you give them the answer then they'll stop thinking for themselves--assuming they thought for themselves in the first place--and expect you to continue giving them the answer. The goal here is not to solve the problems of others, but to teach others how to solve their own problems.

>Do u know how to let the text in the program blink???
I know how to make text blink, but I'm not going to tell you because one method is easy to figure out using what you just learned.

Member Avatar for Geek-Master

Just somthing I made with the bit of info I learned from this thread. It also can be a simulation of those "test" the video broadcasters do for some reason. Let me know what you guys-n-gals think LOL :cheesy:

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

int color();

int main()
{
    int c;
    for (c = 0; c < 24; c++)
    {
    color();
    }
    system("pause");
    return 0;
}

int color()
{
    HANDLE hConsole;
    int k = 0, r;
    for (r = 0; r < 16; r++)
    {
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    k;
    SetConsoleTextAttribute(hConsole, k);
    cout << "ÛÛÛÛÛ";     // Alt 219 (a square) extended ASCII character
    k++;
    }
    return 0;
}

>I think you should leave C++ alone. Go play with QBasic, as it's more your style.
I think you should be silent unless you have something useful to add. Flames are fine as long as they're deserved, but in this case your comments are completely uncalled for.

>Oh, and Dexter's Laboratory sucks.
Personal opinions have no place here unless they're related to C or C++ and carefully worded to make it clear that they're opinions.

If you want to be an ass, at least do it tastefully.

>Let me know what you guys-n-gals think LOL
Well, I think that LOL should only be used in response to something incredibly funny, if at all. Using LOL in response to a statement you make gives the impression that you're a lamer.

Aside from that, the code could be formatted better, it could be reorganized, and you make an assumption about the size of the console window. Here's another way to go about it:

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

void color();

int main()
{
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD windowSize = GetLargestConsoleWindowSize(hConsole);

  for (SHORT c = 0; c < windowSize.Y; c++)
  {
    color();
  }
  std::system("pause");

  return 0;
}

void color()
{
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  WORD k = 0;
  const int colors = 16;

  for (int r = 0; r < colors; r++)
  {
    SetConsoleTextAttribute(hConsole, k++);
    std::cout << "ÛÛÛÛÛ";  // Alt 219 (a square) extended ASCII character
  }
}

All in all, a nice program. Well done. :)

Just somthing I made with the bit of info I learned from this thread. It also can be a simulation of those "test" the video broadcasters do for some reason. Let me know what you guys-n-gals think LOL :cheesy:

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

int color();

int main()
{
    int c;
    for (c = 0; c < 24; c++)
    {
    color();
    }
    system("pause");
    return 0;
}

int color()
{
    HANDLE hConsole;
    int k = 0, r;
    for (r = 0; r < 16; r++)
    {
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    k;
    SetConsoleTextAttribute(hConsole, k);
    cout << "ÛÛÛÛÛ";     // Alt 219 (a square) extended ASCII character
    k++;
    }
    return 0;
}

I hope my somewhat imperfect code got you started! Nice to see that you are thinking in a creative way!

Play nice people. Attitude, name calling, grumpy behavior, or posting without having your Wheaties in the morning will not help us become better coders. And we all should admit that sometimes the documentation / examples that are in the book, or online resource, do not jump out and click.

Locking thread to encourage better use of our time.

Christian

#include <stdio.h>
#include <windows.h>   // or other WinApi header
int main()
{
  HANDLE  hConsole;
    int k;

  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  for(k = 1; k < 25; k++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%2d  %s\n", k, "I want to be nice today!");
  }
  getchar();  // cheap wait
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.