How do I change the color of texts?

Oct 17, 2016 at 9:00pm
Hello!

How do I change the color of the numbers that user enters?
for example how do I make 16, 5 and 9 (Red, blue, yellow etc)?

When you run the program, it should look like this:

Number 1: User enters (16) 16 in red
Number 2: User enters (5) 5 in red
Number 3: User enters (9) 9 in red
16 + 5 + 9 = 30

#include <iostream>
using namespace std;

int main() {

int firstInt;
int secondInt;
int thirdInt;
int sum;

cout << "Number 1: ";
cin >> firstInt;
cout << "Number 2: ";
cin >> secondInt;
cout << "Number 3: ";
cin >> thirdInt;

sum = firstInt + secondInt + thirdInt;
cout << firstInt << " + " << secondInt << " + " << thirdInt << " = " << sum << endl;


return 0;
}
Last edited on Oct 17, 2016 at 9:01pm
Oct 17, 2016 at 9:31pm
Send me a private message.
Oct 17, 2016 at 9:58pm
With standard c++ you can't.
Oct 17, 2016 at 10:17pm
Hello CRAFSHIN,

I have tried that before. In the console window you only have one colour for the foreground and background. In older versions of DOS you could use escape sequence to change the colour. If it is still available I have not found the files necessary to use the escape sequences.

Andy
Oct 17, 2016 at 10:43pm
I hate it when people say you can't... Of course you can that's why we program...
Btw, 0 is black but you can't see black on black so no reason to display it here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Color Picker
#include <iostream>
#include <windows.h>
using namespace std;    

void SetColor(int ColorPicker)
{
WORD consoleColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
	{
	consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
	SetConsoleTextAttribute(hStdOut, consoleColor);
	}
}

int main()
{
	for (int i =1; i <=15; i++)
	{
		SetColor(i);
	    cout  << " This is " << i << endl;
	}
    return 0;
}
Last edited on Oct 17, 2016 at 10:47pm
Oct 17, 2016 at 10:47pm
@SamuelAdams
Maybe it is better if you give him a Color enum so that he can just instantly use it without worrying about the code itself or how to use the code.
Oct 17, 2016 at 11:50pm
He didn't ask for just one color
(Red, blue, yellow etc)?


With that code he can see all the colors and fill in the etc himself.

I'm not sure I'd recommend that code for a beginner doing homework but I was not happy Yanson said you can't change colors. I'm sure there are multiple ways.
Oct 18, 2016 at 12:31am
closed account (E0p9LyTq)
Adding color to your (Windows) console:
http://www.cplusplus.com/articles/Eyhv0pDG/

A Linux console can have color, done in a different way:
http://www.cplusplus.com/forum/unices/36461/
Oct 18, 2016 at 5:57am
I don't understand anything from the code that @SamuelAdams gave.

Guys is there a easier way to change the colors beside that code what @SamuelAdams gave?

Oct 18, 2016 at 9:54am
CRAFSHIN, copy and paste the SetColor function into your project and call it before you want to change the colour of the text. Like this if you wanted your text to be green.

 
SetColor(10);


Oct 18, 2016 at 2:11pm
I'm sorry, I knew it was too complex but shows it is possible. Yes there are many ways, just google to find a few.

The code I provided is a function called SetColor. The part in main prints out the colors 1-15, 0 is black so you wouldn't be able to see it on a black screen.
The colors are

Name | Value
Black | 0
Blue | 1
Green | 2
Cyan | 3
Red | 4
Magenta | 5
Brown | 6
Light Gray | 7
Dark Gray | 8
Light Blue | 9
Light Green | 10
Light Cyan | 11
Light Red | 12
Light Magenta| 13
Yellow | 14
White | 15


Here's one that is easier to understand but not recommended for real programming. ok to learn with.
http://www.cplusplus.com/forum/beginner/5830/
Oct 18, 2016 at 2:44pm
@SamuelAdams

Thank you for the code, but I just don't understand the code, I mean I don't understand the meaning of those lines in that code, because I just started programming and I don't know how to use that code in my project.

Would it be something like this? I don't know...

// Color Picker
#include <iostream>
#include <windows.h>
using namespace std;

void SetColor(int ColorPicker)
{
WORD consoleColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
SetConsoleTextAttribute(hStdOut, consoleColor);
}
}

int main()
{
for (int i =1; i <=15; i++)
{
SetColor(i);
cout << " This is " << i << endl;
}
return 0;
}

int main() {

int firstInt;
int secondInt;
int thirdInt;
int sum;

cout << "Number 1: ";
cin >> firstInt;
cout << "Number 2: ";
cin >> secondInt;
cout << "Number 3: ";
cin >> thirdInt;

sum = firstInt + secondInt + thirdInt;
cout << firstInt << " + " << secondInt << " + " << thirdInt << " = " << sum << endl;


return 0;
}
Oct 18, 2016 at 3:01pm
@SamuelAdams
Do you know how to create an enum?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum
{
    Black = 0,
    Blue = 1,
    Green = 2,
    Cyan = 3,
    Red = 4,
    Magenta = 5,
    Brown = 6,
    Light_Gray = 7,
    Dark_Gray = 8,
    Light_Blue = 9,
    Light_Green = 10,
    Light_Cyan = 11,
    Light_Red = 12,
    Light_Magenta = 13,
    Yellow = 14,
    White = 15
};


And you should not tell him to actually use magic numbers. It is considered bad practice.
Oct 21, 2016 at 5:00pm
I agree, a lot of bad code is written every day. But if he stays in programming long enough he's going to see good and bad examples, and needs exposure to both so he can learn the difference before he actually codes something important.
Topic archived. No new replies allowed.