Console colored text

This works perfect if I do something like print("Hello world", red).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print(const string a, const int b)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),b);
    std::cout << a << std::endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}


int main()
{
    enum { black, blue, green, cyan, red, magenta, brown, lightgrey, darkgrey, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white };
    print("I am red.", red);
    print("I am blue.", blue);
}

This almost works like I want except for I would like to be able to put this function inside of the cout statement I tried std::cout << color(red) << "red" << std::endl; but then I get this error: no match for 'operator<<' in 'std::cout << color(4u)' ('u' because its unsigned if I remove unsigned its same error but without the u)
1
2
3
4
5
6
7
8
9
10
void color(const unsigned int a)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

int main()
{
    enum { black, blue, green, cyan, red, magenta, brown, lightgrey, darkgrey, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white };
    color(red);
    std::cout  << "I am Red." << endl;


How would I make something like this into
std::cout << red << "I am red.\n" << blue << "I am blue." << grey << std::endl;
That is how we do it in class using my teachers header but it is in linux (using ansi escape sequences) so doesn't exactly do me much good =p

Any suggestions would be greatly appreciated(I'm trying to make a header file that will have a bunch of functions that I could use on any program without having to put a bunch of includes like for math formulas, and the std libraries for checking if its a digit or letter and other stuff like that)
Last edited on
closed account (Dy7SLyTq)
create an enum for it and overload << for the enum
could you please give me an example I am a bit confused I thought that enum was to number a list of items ex
1
2
3
4
 enum { apple, banana, orange }
cout << apple << endl;
cout << orange << endl;
cout << grape << endl;


0
1
2


I tried to do enum { black = color(black) } but does not work
not exactly sure what you mean

And I figured out why my code wasn't working =] 1) It was supposed to be a char function not void and 2) I needed to return the color and a backslash to get rid of the '1'
here is the working code if anyone else wants it for fun =]

1
2
3
4
5
6
7
8
9
10
11
char color(const unsigned int a)
{
    return(SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a),'\b');
}

int main()
{
    enum { black, blue, green, cyan, red, magenta, brown, normal, darkgrey, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white };
    cout << color(blue) << "blue" << endl;
    cout << color(normal) << "default" << endl;
}


Thanks for your suggestion though DTSCode

ARGH edit::

there is still a major bug in my code any suggestions would be greatly appreciated I have this:
1
2
3
4
char color(const unsigned int a)
{
    return(SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a),'\b');
}

The bug is if I try and call multiple colors in 1 line it will only be the color of the first line
like this
1
2
3
4
5
 int main()
{
enum { black, blue, green, cyan, red, magenta, brown, normal, darkgrey, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white };
    cout << color(normal) << "normal\n" << color(blue) << "blue\n" << color(red) << "red" << color(normal) << endl;
}

Any suggestions would be greatly appreciated
Last edited on
Topic archived. No new replies allowed.