User Input in the Terminal

Changing Console Input Color in C++

Can I change the color or formatting of the input prompt in the console?

Abstract art representing computer programming

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.

This Question is from the Lesson:

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 59 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright Β© 2024 - All Rights Reserved