font color error

closed account (1vf9z8AR)
I am trying to change the baackground color and the font color in c++ but 'codeblocks' only reads the last system("color _ _")

#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
system("color 3E");
cout<<"hi"<<endl;
system("color 3D");
cout<<"hi";
}



i have tried using different functions but failed. Below is my attempt

#include<iostream>
#include<graphics.h>
using namespace std;
int head1()
{
system("color 3E");
cout<<"hi";
return 0;
}
int main()
{
head1();
system("color 3E");
cout<<"hi";
}
this doesnt work too.

Last edited on
Don't you need to include cstdlib?
#include <cstdlib>
Last edited on
Hello suyashsing234,

Awhile back I once told someone this could not be done. Then I was told it could be done and was presented this code:

1
2
3
4
5
6
7
8
9
10
11
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);
	}
}  //  End SetColor() 


As I remember this requires the header file "Windows.h" to work. This will change the font colour, but not the background. I have played with this code to change the background color, but it is not easy to do.

I do not have the "graphics.h" header file, so I am not sure how to use it.

I do not believe that the "graphics.h" file is needed to use "system("color 3E");". At least I have not needed anything special to use "system("color 3E");".

Some day I will find the function taht allows the foreground and background colours to be changed.

Hope that helps,

Andy
Am I missing the point? The following code changes my W7 background console colour twice:

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
27
#include <cstdlib>
#include <iostream>
#include <limits>


void waitForEnter();


int main()
{
    system("color 3E");
    std::cout << "hi\n";
    waitForEnter();

    std::cout << "hi again\n";
    system("color 3D");

    waitForEnter();
    return 0;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

@Enoizat

I believe the OP wants different color texts and/or backgrounds on the screen at the time, not just change all the text or background to a different color.

@Handy Andy
This code does change background and/or text, as needed.

@suyashsing234
Here is a small program I wrote a few years back as a text/background colors demo. I hope you're able to use it, as well.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <windows.h>

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using std::cout;
using std::endl;
using std::cin;
using std::string;

#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

int main()
{
	string Color[16] = { "Black", "Dark Blue", "Dark Green", "Dark Cyan", "Dark Red", "Dark Magenta", "Dark Yellow", "Light Gray",
		"Dark Gray", "Light Blue", "Light Green", "Light Cyan", "Light Red", "Light Magenta", "Light Yellow", "White" };
	int i, j;

	for (j = 0; j<16; j++)
	{
		for (i = 0; i<16; i++)
		{
			if (j == i) // If text color is same as paper color
			{
				text();
				cout << "---> *** Place-holder --- Text same color as background *** <---" << endl;
			}
			else
			{
				text(i on j);
				cout << Color[i] << " on " << Color[j] << " (Text color=" << i << "+Background color=" << j * 16 << " (" << j << "*16)=" << i + (j * 16) << ")" << endl;
			}
		}
	}
	text(); // Able to use names here, because of enum Colors{};

	cout << endl << endl << "Where a number is skipped, the text would be same color as the backgound.." << endl;
	cout << "To use color, add to your list of ";
	text(light_red on black);
	cout << "#include";
	text();
	cout << "'s, if not already used.." << endl;
	text(light_red on black);
	cout << "#include <windows.h>";
	text();
	cout << "." << endl << "Declare the following, afterwards.." << endl;
	text(light_red on black);
	cout << "HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);" << endl;
	text();
	cout << "and use the folowing, whenever you wish to change colors.." << endl;
	text(light_red on black);
	cout << "SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );" << endl;
	text();
	cout << "Or examine the '";
	text(light_red on black);
	cout << "void text(int text_color on int paper_color);";
	text();
	cout << "' function," << endl << "at the top of this program. Works great, doesn't it ??" << endl;
	cout << "Just make sure to add '";
	text(light_red on black);
	cout << "#define on ,";
	text();
	cout << "', if you want to use the '";
	text(light_red on black);
	cout << "on";
	text();
	cout << "' word," << endl;
	cout << "instead of a comma, to separate the two color choices." << endl << endl;
	cout << "Press enter to exit.." << endl;
	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}
Last edited on
Hello @whitenite1,

Thanks for the input. That is one direction that I had not tried yet, The way you used the "text function is the change I needed.

Andy
closed account (1vf9z8AR)
umm guys i will stick to a constant color. I cant really use big and complex coding in my project for school.
Thanks though for your responses. :)
Hello suyashsing234,

Instead of the "#include <graphics.h>" try "#include <Windows.h>" I think that is the header file needed to use "system". The "graphics.h" file should have its own function(s) for changing the foreground and background colours.

When I tried "system("color 3E");" in one of my programs it worked and at the end of the program when I set the colours back to what I normally use it worked.

Hope that helps,

Andy
Hello suyashsing234,

Upon further investigation I found the proper header file for "system" is "cstdlib" for C++ or "stdlib.h".

Andy
closed account (1vf9z8AR)
Thankyou Handy Andy it works with cstdlib.
Hello suyashsing234,

Your welcome.

Sometimes it is the little things that takethe longet to find.

Andy
Topic archived. No new replies allowed.