is <windows.h>'s system("color 01") cool to use?

Hey guys,I was just getting to know formatting output options in c++ and came upon the color() in windows library.
Thing is i've been told many times to avoid using system() but here i can't find any alternative to color my output.

Can you guys justify why many people don't like using system() and are there any alternatives to system("color 01").

Thank you.

well, there are reasons.
I will list a few:
1. system() is not portable i.e it is windows only
2. for some people it triggers something about their antivirus
though it is bad to use it, I wouldn't really say it is bad when used in a really small project and you are not planning to port it. there are alternatives to system( "color 01" ); though.
cplusplus.com/forum/beginner/5830
The color has nothing to do with C++. It doesn't even have anything to do with "Windows library"*.

1. Open Windows Terminal (the cmd).
2. Type in terminal: help color

The color is (at least effectively) an entirely separate MS-specific program that does send instructions to the MS GUI terminal window. Your program happens to / attempts to execute that external program.


This program uses "a library":
1
2
3
4
5
#include <iostream>

int main() {
  std::cout << "Hello\n";
}

The std::cout is a global variable that is declared and defined elsewhere. We can use it, because we include header "iostream" into our source code and link the library file(s) with our binary.

The << is a function that we call, and its implementation in linked library will do the work. The implementation will interact with terminal window to pass the string for display rendering.


The "Windows library" may or may not have functions that you could call in order to modify the colour attributes of the GUI window of the terminal, where your program happens to run.

Alas, CMD terminal is not the only implementation of terminal. Most of the other programs understand common formatting instructions, but CMD is not one of them. Do note that even though MS Windows dominates (some) market, most users of MS Windows do not use the CMD terminal. Ever.

Therefore, your approach to "format output" is limited to a niche product.

There are third party libraries that have been ported to translate "GUI formatting" to multiple terminal types, including the CMD.


*Our concept of what is a "library" might differ.
Last edited on
well, just to put it out there, there are also the ncurses library ( primarily for *nix, but ports for windows exist ). With it, you can do what yoou want, though it is going to be more complicated as it involved manually linking your code to the library ( not that hard )
Topic archived. No new replies allowed.