Подчеркивание на C ++

Как я могу подчеркнуть текст, который должен быть вывод кода C ++?

Где-то в сети я видел это:

cout<<underline<<"This is the text which is going to be underlined.";

Но для меня это «подчеркивание» не работает. Любая идея очень приветствуется.

3

Решение

Вы выводите на терминал ANSI? Если это так, должна работать следующая escape-последовательность:

#define underline "\033[4m"

Более подробная информация о escape-последовательностях ANSI доступна здесь.

Примечание: чтобы снова отключить подчеркивание, используйте "\033[0m",

8

Другие решения

Вероятно, самый простой и переносимый метод — это просто так:

cout << "This is the text which is going to be underlined." << endl;
cout << "-------------------------------------------------" << endl;
5

Ниже приведен обширный пример, написанный для G ++:

#include <iostream>
using namespace std;
int  main()
{
char normal[]={0x1b,'[','0',';','3','9','m',0};
char black[]={0x1b,'[','0',';','3','0','m',0};
char red[]={0x1b,'[','0',';','3','1','m',0};
char green[]={0x1b,'[','0',';','3', '2','m',0};
char yellow[]={0x1b,'[','0',';','3', '3', 'm',0};
char blue[]={0x1b,'[','0',';','3','4','m',0};
char Upurple[]={0x1b,'[','4',';','3','5','m',0};
char cyan[]={0x1b,'[','0',';','3','6','m',0};
char lgray[]={0x1b,'[','0',';','3','7','m',0};
char dgray[]={0x1b,'[','0',';','3','8','m',0};
char Bred[]={0x1b,'[','1',';','3','1','m',0};
//for bold colors, just change the 0 after the [ to a 1
//for underlined colors, just change the 0 after the [ to a 4
cout<<"This text is "<<black<<"Black "<<red<<"Red ";
cout<<green<<"Green "<<yellow<<"Yellow "<<blue<<"Blue\n";
cout<<Upurple<<"Underlined Purple "<<cyan<<"Cyan ";
cout<<lgray<<"Light Gray "<<dgray<<"Dark Gray ";
cout<<Bred<<"and Bold Red."<<normal<<"\n";
return 0;
}
2

Чтобы завершить ответ Пола Р., я иногда создаю эту функцию в своих программах:

std::string underline(const std::string &s) {
return std::string(s.length(), '-');
}

Тогда вы можете сделать:

int main() {
constexpr auto TEXT = "I am underlined";
std::cout << TEXT << std::endl << underline(TEXT) << std::endl;
return 0;
}

другие возможности:

void underlineAndDisplay(const std::string &s);
std::string underlineWith(const std::string &s, char c);

Хорошо, давайте вернемся к моему коду …

1
По вопросам рекламы [email protected]