Change color of the text that user inputs when prompted .

So when the user is prompted to input data using their keyboard. That input appears on the screen. I want to change the color of just that text to easily distinguish between user input and program output.
1
2
3
string color;
cout << "Please type in your favorite color: "; // it actually will appear here
cin >> color; cout << endl; // right here is where the user input would appear on the console... Any way to change the color of that text only???? Please help me C++ gods! 
Last edited on
If you are a victim of Windows tyranny here's an oldish article for changing the colors in the console:

http://www.cplusplus.com/articles/Eyhv0pDG/
Thanks. I will check it out.
Hello mdh1559,

The code used in the article is nice, but a bit long. You should find this moer to what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// <--- Prototype.
void SetColor(int foreground = 7, int background = 1);

void SetColor(int foreground, int background)
{
	WORD consoleColor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
	{
		//consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
		consoleColor = (foreground + (background * 16));
		SetConsoleTextAttribute(hStdOut, consoleColor);
	}
}  //  End SetColor() 

When I use this I usually put the prototype in a separate header file, but if you are not ready for that you can put this all in one file.

The prototype is designed to provide a default "foreground" and "background" colour. You can change this if you like.

You can also add something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CONCOL
#define CONCOL
	enum concol
	{
		black=0,
		dark_blue=1,
		dark_green=2,
		dark_aqua,dark_cyan=3,
		dark_red=4,
		dark_purple=5,dark_pink=5,dark_magenta=5,
		dark_yellow=6,
		dark_white=7,
		gray=8,
		blue=9,
		green=10,
		aqua=11,cyan=11,
		red=12,
		purple=13,pink=13,magenta=13,
		yellow=14,
		white=15
	};
#endif //CONCOL 

Wherever you put the prototype.

For use:
1
2
3
4
5
6
7
8
9
SetColor(14);

std::cout << "A prompt for something: ";

SetColor(10);

std::cin >> aVariable;

SetColor();

Line 1 sets the foreground colour to "yellow" then outputs the prompt on line 3 in "yellow". Line 5 changes the foreground colour to a light blue so that the input is in a different colour. Both of these use the default background colour set in the prototype.

Line 10 with the empty () used both of the default colours to reset the the foreground and background colours.

It is a lot of work, but you can change the colour of each letter if you want or by word and as the example shows by line. If you change the colours and clear the screen then it will affect everything on the screen.

I do not know how the code in the article works. It may end up working the same since I have not tried it yet.

I found this code to replace "system("cls");"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Standard error macro for reporting API errors */
#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n", \
__FILE__, GetLastError(), api, __LINE__);}
//#define PERR(bSuccess, api){if(!(bSuccess)) std::cout << __FILE__ << ", " << GetLastError() << ", " << api << ", " << __LINE__);}

void cls()
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coordScreen = { 0, 0 };    /* here's where we'll home the
										cursor */
	BOOL bSuccess;
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
	DWORD dwConSize;                 /* number of character cells in
										the current buffer */

	/* get the number of character cells in the current buffer */

	bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);

	PERR(bSuccess, "GetConsoleScreenBufferInfo");

	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

	/* fill the entire screen with blanks */

	bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
		dwConSize, coordScreen, &cCharsWritten);

	PERR(bSuccess, "FillConsoleOutputCharacter");

	/* get the current text attribute */

	bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
	PERR(bSuccess, "ConsoleScreenBufferInfo");

	/* now set the buffer's attributes accordingly */

	bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
		dwConSize, coordScreen, &cCharsWritten);

	PERR(bSuccess, "FillConsoleOutputAttribute");

	/* put the cursor at (0, 0) */

	bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
	PERR(bSuccess, "SetConsoleCursorPosition");

	return;
}  //  End CLS 

Line 4 was an attempt to rewrite the previous, but it did not work and I stopped working on it.

I also use this #define CLS cls() so all I have to enter into the code is CLS; which, if I have this right, is expanded by the processor to the function call.

With both of these functions you will need to include "Windows.h". This can be a problem if you also include "limits".

Eventually what I was told is to do this:
1
2
3
4
#include <Windows.h>
#undef min
#undef max
#include <limits> 

The "#undef"s are because when including "Windows.h" "min" and "max" are defined, but "limits" has its own definition for "min"and "max" that is different.

If you do not include "limits" in the program, more likely you will, you will not need the "#undef"s unless it should cause a problem.

Hope that helps,

Andy
Topic archived. No new replies allowed.