How do you change the color of text being printed into the console from my program?
How do you change the color of text being printed into the console from my program?
Jacob Sheehy
i would also like to know, and how to do it on msvc++ if anyone knows?
Paro
Syntax
#include <conio.h>
void textcolor(int _color);
Description
Sets just the foreground of the text attribute.
There. Easy as that. Make sure to put a clrscr() after you change the color, otherwise there will be no effect. Look through the conio.h library to see all of the colors. There are some light versions of colors as well, so make sure to check the library. Hope that helps.Code:#include <iostream.h> #include <conio.h> int main { textcolor(LIGHTBLUE); // Change font color to Light Blue textbackground(RED); // Change background color to Red clrscr(); // Needed before changes will take effect cout<< "Hello.\n"; textcolor(LIGHTGRAY); // Default DOS Prompt text color textbackground(BLACK); // Default DOS Prompt background color clrscr(); return(0); }
Kyoto Oshiro
Horizon Games
http://www.angelfire.com/realm2/horizon/files.html
I am getting some wierd compiler errors from running that program:
I cant seem to get the colors to work
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x19):redcolor.cp p: undefined reference to `textcolor'
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x26):redcolor.cp p: undefined reference to `textbackground'
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x2e):redcolor.cp p: undefined reference to `clrscr'
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x4d):redcolor.cp p: undefined reference to `textcolor'
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x5a):redcolor.cp p: undefined reference to `textbackground'
C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x62):redcolor.cp p: undefined reference to `clrscr'
whats wrong? Im using Dev-C++
ok, i copy and pasted that...
added "()" after main so it would work...
added the code for clrscr() to work
(code i used above)Code:#include <iostream.h> #include <conio.h> #include <windows.h> void clrscr(); int main () { textcolor(LIGHTBLUE); // Change font color to Light Blue textbackground(RED); // Change background color to Red clrscr(); // Needed before changes will take effect cout<< "Hello.\n"; textcolor(LIGHTGRAY); // Default DOS Prompt text color textbackground(BLACK); // Default DOS Prompt background color clrscr(); return(0); } void clrscr() { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); }
i use MSVS 6.0 btw...
and i compile and this is what i get
Any clues?--------------------Configuration: PrimeNumber - Win32 Debug--------------------
Compiling...
Prime.cpp
C:\Programming\PrimeNumber\Prime.cpp(8) : error C2065: 'textcolor' : undeclared identifier
C:\Programming\PrimeNumber\Prime.cpp(8) : error C2065: 'LIGHTBLUE' : undeclared identifier
C:\Programming\PrimeNumber\Prime.cpp(9) : error C2065: 'textbackground' : undeclared identifier
C:\Programming\PrimeNumber\Prime.cpp(9) : error C2065: 'RED' : undeclared identifier
C:\Programming\PrimeNumber\Prime.cpp(14) : error C2065: 'LIGHTGRAY' : undeclared identifier
C:\Programming\PrimeNumber\Prime.cpp(15) : error C2065: 'BLACK' : undeclared identifier
Error executing cl.exe.
PrimeNumber.exe - 6 error(s), 0 warning(s)
Paro
I am using the free v 5.5 compiler off the Borland website. I do all my colors this way, and it seems to work fine. Then again, I don't do all that in the void clrscr() stuff Paro. And sorry about the missing () after int main...not enough sleep
If that doesn't work, then I don't know. I have never used either of the compilers you have mentioned, so that might be a reason. Wierd.Code:#include <iostream.h> #include <conio.h> int main() { textcolor(WHITE); textbackground(BLACK); clrscr(); cout<< "\aHello.\n\n"; return(0); }
Kyoto Oshiro
it must be the compiler...cuz it just wont work for me
and i thought msvc++ was better than the common freebie!
This has been asked many times. Conio.h is a non-standard header so the ones that have made the compiler has, in your case, probably decided not to take most of its functions.
Above you say things like "LightGrey". Can I use either HEX or RGB to define the colors?
thanks,
Greg
Chance favors the prepared mind.
Vis. C++ 6.0
So how is it done with VC++?
Jacob Sheehy
Colors in console for msvc:
Guys this has been posted like a thousand times,a search would have found it.Code:the colors can be mixed FOREGROUND_BLUE Text color contains blue. FOREGROUND_GREEN Text color contains green. FOREGROUND_RED Text color contains red. FOREGROUND_INTENSITY Text color is intensified. BACKGROUND_BLUE Background color contains blue. BACKGROUND_GREEN Background color contains green. BACKGROUND_RED Background color contains red. BACKGROUND_INTENSITY Background color is intensified. #include <windows.h> #include <stdio.h> int main( void ) { HANDLE hStdout; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hStdout, FOREGROUND_RED); printf("WWWWOOOOO MY TEXT IS RED WWOOOOO!!"); return 0; }
All spelling mistakes, syntatical errors and stupid comments are intentional.
Yep, or try these:
http://www.cprogramming.com/cboard/s...3&pagenumber=2
http://sourceforge.net/forum/forum.p...forum_id=48211
Only there is no conio_mingw.h in Visual C...
Hey, sorry for not searching, I'm usually very good about that. Sorry. Your code works great, thank you! When you say that "the colors can be mixed", how do you do that? thanks
Jacob Sheehy