使用'系统(" COLOR [attr]")'在C ++中

时间:2014-06-26 11:16:30

标签: c++ colors

我正在编写一个代码,我需要根据用户提供的输入以不同的颜色显示输出。 我怎样才能做到这一点? 尝试使用以下代码,但它无效。

#include<iostream>
#include<cstdlib>
using namespace std;

int main(void)
{       
    system ( "TITLE Color Check" );
    for(int i=0;i<10;i++)
    {
         system ( "COLOR i" );
         cout << "This color is COLOR" << i << endl;
    }


    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这应该有效:

for(int i = 31; i < 38; i++) 
    cout << "\e[0;" << i << "m"<< "This color is COLOR " << i << "\e[0;0m" << endl;

只需使用正确的颜色代码:

  • '\ e [0; 31m'#Red
  • '\ e [0; 32m'#Green
  • '\ e [0; 33m'#Yellow
  • '\ e [0; 34m'#Blue
  • '\ e [0; 35m'#Purple
  • '\ e [0; 36m'#Cyan
  • '\ e [0; 37m'#White