0

I have the following code copied from https://www.geeksforgeeks.org/how-to-print-colored-text-in-c/. I used the exact same code in replit and VS Code. The VS Code runs fine but in replit I get the following error message.

In the console:

error message console

In the shell:

error message shell

How can I get my text highlighted?

#include <iostream>
#include <stdlib.h>
using namespace std;

// Driver Code
int main()
{
// B for background Color(Light Aqua)
// 5 for text color(Purple)
system("Color B5");
cout << "Geeks";

// 1 for background Color(Blue)
// 6 for text color(Yellow)
system("Color 16");
cout << " For ";

// D for background Color(Light Purple)
// E for text color(Light Yellow)
system("Color DE");
cout << "Geeks";

return 0;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Pygatti
  • 1
  • 1
  • In the shell, it's a warning saying you're not using the return value; you can ignore it there. – ChrisMM Apr 20 '22 at 16:04

1 Answers1

2

Color is a cmd.exe built-in; it doesn't work anywhere but a DOS-like prompt (not even Powershell). replit is clearly running your program in sh, a shell for UNIX-like systems; it doesn't have the command. The guide you are reading is implicitly written solely for Windows systems; it won't work on UNIX-likes (e.g. Linux, BSD, macOS).

A somewhat more portable solution uses ANSI color codes, which almost all modern terminals support (may not work in cmd.exe without some configuration though; it does work in the VSCode terminal), and doesn't require launching subprocesses for every single color change. If you want full portability, libraries exist, e.g. ncurses, which has been ported to Windows and provides complete terminal control support, independent of the actual OS, terminal and shell involved.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271