Changing Console Input Color in C++
Can I change the color or formatting of the input prompt in the console?
Yes, you can change the color and formatting of the input prompt in the console using C++.
However, the method to do this is platform-dependent. We'll explore how to do this for both Windows and Unix-like systems (Linux, macOS), and then provide a cross-platform solution.
Windows Implementation
On Windows, we can use the SetConsoleTextAttribute()
function from the Windows API to change text color:
#include <iostream>
#include <string>
#include <windows.h>
void setColor(int color){
SetConsoleTextAttribute(
GetStdHandle(STD_OUTPUT_HANDLE),
color);
}
int main(){
setColor(FOREGROUND_RED |
FOREGROUND_INTENSITY); // Bright red
std::cout << "Enter your name: ";
setColor(FOREGROUND_GREEN | FOREGROUND_BLUE |
FOREGROUND_INTENSITY); // Cyan
std::string name;
std::getline(std::cin, name);
setColor(FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE); // White
std::cout << "Hello, " << name << "!\n";
}
Enter your name: RyanHello, Ryan!
In this Windows example, we define a setColor()
function that uses SetConsoleTextAttribute()
to change the console text color. We use different color combinations for the prompt and the input.
Unix-like Systems Implementation
For Unix-like systems, we can use ANSI escape codes to change text color:
#include <iostream>
#include <string>
// ANSI color codes
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BLUE "\033[34m"
#define CYAN "\033[36m"
int main(){
std::cout << RED << "Enter your name: " <<
CYAN;
std::string name;
std::getline(std::cin, name);
std::cout << RESET << "Hello, " << GREEN <<
name << RESET
<< "!\n";
}
Enter your name: RyanHello, Ryan!
In this Unix-like system example, we use ANSI escape codes to change text colors. These codes are supported by most modern terminal emulators.
Cross-platform Solution
To create a cross-platform solution, we can use conditional compilation and create a simple Color enum:
#include <iostream>
#include <string>
enum class Color {
Red,
Green,
Blue,
Cyan,
Reset
};
#ifdef _WIN32
#include <windows.h>
void setColor(Color color){
HANDLE hConsole = GetStdHandle(
STD_OUTPUT_HANDLE);
switch (color) {
case Color::Red:
SetConsoleTextAttribute(
hConsole,
FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
case Color::Green:
SetConsoleTextAttribute(
hConsole,
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case Color::Blue:
SetConsoleTextAttribute(
hConsole,
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case Color::Cyan:
SetConsoleTextAttribute(
hConsole,
FOREGROUND_GREEN | FOREGROUND_BLUE |
FOREGROUND_INTENSITY);
break;
case Color::Reset:
SetConsoleTextAttribute(
hConsole,
FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE);
break;
}
}
#else
void setColor(Color color) {
switch (color) {
case Color::Red:
std::cout << "\033[31m";
break;
case Color::Green:
std::cout << "\033[32m";
break;
case Color::Blue:
std::cout << "\033[34m";
break;
case Color::Cyan:
std::cout << "\033[36m";
break;
case Color::Reset:
std::cout << "\033[0m";
break;
}
}
#endif
int main(){
setColor(Color::Red);
std::cout << "Enter your name: ";
setColor(Color::Cyan);
std::string name;
std::getline(std::cin, name);
setColor(Color::Reset);
std::cout << "Hello, ";
setColor(Color::Green);
std::cout << name;
setColor(Color::Reset);
}
Enter your name: RyanHello, Ryan!
This cross-platform solution uses conditional compilation to choose the appropriate color-setting method based on the target platform. It provides a consistent interface for changing text colors across different operating systems.
Remember that color support can vary depending on the terminal or console application being used. Some environments may not support colors at all, while others might support a wider range of colors than what's shown here.
Also, keep in mind that changing text colors should be used judiciously. Overuse can make your application harder to read or use, especially for users with visual impairments. Always consider accessibility when designing your user interface.
User Input in the Terminal
This lesson introduces the fundamentals of capturing user input, using std::cin
and std::getline()