how to make color output in C++

Started by
7 comments, last by Maverick Programmer 15 years, 8 months ago
I was wondering how to make color output in C++ in a console app??
Advertisement
Only some window consoles can pull this off. I believe it would go something like this?
int main(){     system("COLOR C");     std::cout << "I'm a different color!\n";     system("COLOR A");     std::cout << "And I'm another color as well.\n";     system("PAUSE");     return 0;}


Look up on Google the different colors, but I think that's how you would go about it.

~Maverick
Holy crap, you can read!
Well That doesn't work I was wondering if there is another way??
C++ doesn't have the ability to make color output.

What you need to do is find a library which can support what you're looking for. I'd honestly use a system specific library before I'd ever use a System() to do something.

If using a console in Windows, you can use the function SetConsoleTextAttribute.

*EDIT*

An example:

#include <windows.h>#include <iostream>int main(){	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	SetConsoleTextAttribute(hConsole,FOREGROUND_RED);	std::cout << "Hello Red World" << std::endl;	return 0;}


[Edited by - Nytegard on July 27, 2008 11:58:50 PM]
You can directly access the video memory:

char far *v=(char far *) 0xB8000000;void printc(char c,int row,int colum,int color){	*(v+((160*row)+(2*colum)))=c;	*(v+((160*row)+((2*colum)+1)))=color;}
Also, color values for text with a black background range from 0-15, so if you want to the change text color constantly, you can play around with color values between 0-15.

#include <windows.h>#include <iostream>int main(){HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);int color = 0, i;for (i = 0; i < 16; i++){ SetConsoleTextAttribute(hConsole, color); std::cout << "I'm a different color!" << std::endl; color++;}system("pause");return 0;}


Also, you can change the background color like this.
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | BACKGROUND_GREEN);std::cout << "Blue text with green background" << std::endl;
Alright, I'm probably going to be attacked on this, but

DO NOT USE system("pause")!!! Heck, like I stated before, try avoid anything that uses system().

Try something else like

#include <iostream>#include <limits>#ifdef _MSC_VER#undef max#endifint main(){	std::cout << "Press Enter to continue..." << std::endl;	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');	return 0;}


It's late, so I'm not going to go further into this, but if you need to know why, search for one of the many threads on system(pause).
Quote:Original post by Vaayu
You can directly access the video memory:

*** Source Snippet Removed ***


You can, but you'd have to be using C and using DOS :)

Well it's in a beginner's section. I understand the concern, but it's like lecturing a new programmer on using polymorphism when he/she had just learned about classes. :/

They'll get there. :]

Did you get what you wanted working, mattnenterprise?
Holy crap, you can read!

This topic is closed to new replies.

Advertisement