Public Group

# C++ Console Colored Text or Alternative?

This topic is 4870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey I am doing a project for school and everyone else in the class is doing it in QBASIC. Now me on the other hand wants and has the ability to do it in C++ (so I can show off with linked lists, etc) because this is my final year of highschool I thought I'd try scrape up as many marks from in school assesments possible. The only thing I'm worried about is in QBASIC everyone can make things all lovely with colors and neatly placed text (something the teacher highly regards) so I figure I want to move beyond simple console based C++ and I think it would be wishful thinking to say that there would be an easy library to use to achieve Console Colored Text in C++. So what should I do? Sure I could just do it in QBASIC like the rest of them, which is what I will do if I can't obtain a solution to my problem but really the simple concept problem isn't even suited to such a simple programming language (the first wall I hit was when you can't create a new TYPE that holds an array inside). So yeah, any suggestions? I can do the entire program easily it's just the presentation I'm going to be lacking... So any suggestions would be much appreciated, thank you all for your time! Take it easy, Jemburula

##### Share on other sites
u didnt mention the OS you're running on so its a gamble. If u're using windows (highly likely), then there's a collection of functions on console formatting. However, these use the Win32 API and might be a bit complicated at first.

For exact details, check out MSDN.

##### Share on other sites
Yes Windows XP. Thanks for the info, keep it coming everyone but I will look into it. Thanks :)

##### Share on other sites
unders dos/windows there is (there was) a non-standard library called conio.h that let you handle coloured texts (as well as other things). I don't know if it is providen with all compilers, nor if it is supported anymore.
Other than that (if you can) you could use win32 functions.

EDIT: if you want to skip win32 functions and you're using mingw, then you may try this conio library.

##### Share on other sites
You can use SetConsoleTextAttribute() to change the colour of console text in Win32, but you'll have to include the window.h header.

Take a look at this quick example I made:
#include <iostream>#include <windows.h>using namespace std;int main(){    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);    cout<<"Moo\n";        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);    cout<<"Moo\n";        cin.get();        return 0;}

As you can see, changing the 2nd parameter changes the colour of the text. Use an RGB colour mixing chart to see which colours used together will make what.

Good luck!

##### Share on other sites
Ah ha! that was perfect. Much appreciated Undead my friend, thank you all for your help but that was fairly simple, I've always tended to avoid things like that in C++ because many I've heard believe such Windows functions are slow, over complicated, etc. But this suits my purpose nicely :) Thankyou!

##### Share on other sites
More colors!!
#include <iostream>#include <windows.h>using namespace std;enum ConsoleTextColor{	GREY,	WHITE,	RED,	GREEN,	BLUE,	PURPLE,	CYAN,	BRIGHT_RED,	BRIGHT_GREEN,	BRIGHT_BLUE,	LAVANDER,	AQUA,	YELLOW,	GOLD};void SetConsoleTextColor(ConsoleTextColor cl){	WORD newColor;	HANDLE ScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);	switch(cl)	{	  case GREY:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;		  break;	  case WHITE:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case RED:		  newColor = FOREGROUND_RED;		  break;  	  case GREEN:		  newColor = FOREGROUND_GREEN;		  break;  	  case BLUE:		  newColor = FOREGROUND_BLUE;		  break;	  case PURPLE:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE;		  break;	  case CYAN:		  newColor = FOREGROUND_GREEN | FOREGROUND_BLUE;		  break;	  case BRIGHT_RED:		  newColor = FOREGROUND_RED | FOREGROUND_INTENSITY;		  break;	  case BRIGHT_GREEN:		  newColor = FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case BRIGHT_BLUE:		  newColor = FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case LAVANDER:		  newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case AQUA:		  newColor = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;		  break;	  case YELLOW:		  newColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;		  break;	  case GOLD:		  newColor = FOREGROUND_RED | FOREGROUND_GREEN;		  break;	}	SetConsoleTextAttribute(ScreenBuffer, newColor);}int main(){	ConsoleTextColor textColor;	for(int i = 0; i<=GOLD; i++)	{		textColor = (ConsoleTextColor)i;		SetConsoleTextColor(textColor);		cout << "Moo." << endl;	}    return 0;}

##### Share on other sites
Awesome! BTW, does anyone have any thoughts on a LOCATE statement equivelent? If you've never used QBASIC before it essentially just sets the "cursor" or "pointer" to which ever Row and Column you want on screen and then when you next print it writes from there?

##### Share on other sites
I believe you might want SetConsoleCursorPosition();

This may not interest you at the current time since you've got a project to do, but this console tutorial by Ben Ryves is also very interesting.

##### Share on other sites
This link is to the complete reference of the Win32 console API. It should be able to handle any such questions.

Very handy stuff for Windows ASCII games.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 24
• 15
• 36
• 12
• ### Forum Statistics

• Total Topics
634823
• Total Posts
3019459
×