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: Ryan
Hello, 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: Ryan
Hello, 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: Ryan
Hello, 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()

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Formatting Console Output in C++
How can I format console output to create more visually appealing displays?
Secure Password Input in C++
How do I handle password input where characters should not be displayed?
Handling Arrow Key Input in C++
Is it possible to get arrow key input in C++ for navigation purposes?
Validating User Input in C++
How can I validate user input to ensure it's the correct data type?
Multiple Inputs on a Single Line in C++
Is it possible to input multiple values in a single line using std::cin?
Handling Spaces in User Input
How do I handle spaces in user input when using std::cin?
std::cin vs std::getline() in C++
What's the difference between using std::cin >> and std::getline()?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant