Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold output in C++

Tags:

c++

string

I'm building a dictionary, and when I print(output) the word-defenitions I'd like to print the word itself in bold. when I print

cout<<word<<endl<<defention1<<defenition2<<endl;

I want the only "word" to be bold.

How can I do that?

like image 591
KitKat Avatar asked May 02 '15 00:05

KitKat


1 Answers

Standard C++ uses various locales/character sets to display the output in various alphabets. However, the text itself is just that, text, without formatting.

If you want your output to be colored, or bold, or italicized, then you need to send an appropriate character code to your terminal.

However, this is implementation-defined and not guaranteed to work on all platforms.

For example, in Linux/UNIX you may use ANSI escape codes if your terminal supports them.

Example that works on my Mac OS X:

#include <iostream>

int main()
{
    std::cout << "\e[1mBold\e[0m non-bold" << std::endl; // displays Bold in bold
}

If you want, you can go an extra step and create manipulators for turning on/off the bold-ing:

#include <iostream>

std::ostream& bold_on(std::ostream& os)
{
    return os << "\e[1m";
}

std::ostream& bold_off(std::ostream& os)
{
    return os << "\e[0m";
}


int main()
{
    std::cout << bold_on << "bold" << bold_off << " non-bold" << std::endl; 
}
like image 115
vsoftco Avatar answered Sep 24 '22 12:09

vsoftco