changing the backgroud to white while certain part appear to be black, music does not play

How can i change the background in this code? I am doing an ASCII art of horse for my project. While i want to change the background color to white, some of the background appear as black.(I am changing from the properties there) Or maybe other method for me to change my background to white? Also, why does the music does not play? Anyone please help me. Thanks in advance.

#include <iostream>
#include <windows.h>
#include <MMSystem.h>
using namespace std;
char* Color(int color=13, char* Message = "")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
return Message;
}
int main()
{
cout<<" "<<endl;
cout<<Color(8, " ######## ")<<endl;
cout<<Color(8, " ############## ")<<endl;
cout<<Color(8, " #####");
cout<<Color(6, "*");
cout<<Color(8, "#");
cout<<Color(4, "*****");
cout<<Color(8,"####### ")<<endl;
cout<<Color(8," ## ####");
cout<<Color(4,"*");
cout<<Color(8,"#");
cout<<Color(6,"*");
cout<<Color(8,"#");
cout<<Color(4,"**********");
cout<<Color(8,"### ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"*");
cout<<Color(8,"###");
cout<<Color(4,"***");
cout<<Color(8,"#");
cout<<Color(6,"*");
cout<<Color(8,"#########");
cout<<Color(4,"****");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"**");
cout<<Color(8,"##");
cout<<Color(4,"**");
cout<<Color(8,"#");
cout<<Color(6,"**");
cout<<Color(8,"#");
cout<<Color(6,"******");
cout<<Color(8,"####");
cout<<Color(4,"***");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"*");
cout<<Color(8,"##");
cout<<Color(4,"**");
cout<<Color(8,"#");
cout<<Color(6,"**");
cout<<Color(8,"#");
cout<<Color(6,"*********");
cout<<Color(8,"###");
cout<<Color(4,"**");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," ###");
cout<<Color(4,"**");
cout<<Color(8,"#");
cout<<Color(6,"**************");
cout<<Color(8,"##");
cout<<Color(4,"**");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," #");
cout<<Color(4,"**");
cout<<Color(8,"##");
cout<<Color(6,"****************");
cout<<Color(8,"##");
cout<<Color(4,"*");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," #");
cout<<Color(4,"**");
cout<<Color(8,"#");
cout<<Color(6,"*******************");
cout<<Color(8,"#");
cout<<Color(4,"*");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," ##");
cout<<Color(4,"*");
cout<<Color(8,"#");
cout<<Color(6,"********************");
cout<<Color(8,"### ")<<endl;
cout<<Color(8," ####");
cout<<Color(6,"**********************");
cout<<Color(8,"## ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"***********");
cout<<Color(8,"#");
cout<<Color(6,"**************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"*****");
cout<<Color(7,"#");
cout<<Color(8,"#");
cout<<Color(6,"***");
cout<<Color(8,"##");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"*****");
cout<<Color(8,"###");
cout<<Color(6,"***");
cout<<Color(8,"#");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"*****");
cout<<Color(8,"##");
cout<<Color(7,"#");
cout<<Color(6,"***");
cout<<Color(8,"#");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"***********");
cout<<Color(8,"#");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"***********");
cout<<Color(8,"#");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(6,"**********");
cout<<Color(8,"##");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ");
cout<<Color(6,"********");
cout<<Color(8,"#");
cout<<Color(6,"**************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ");
cout<<Color(6,"*****");
cout<<Color(8,"# #");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ");
cout<<Color(6,"***");
cout<<Color(8,"# #");
cout<<Color(6,"*************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # # #");
cout<<Color(6,"************");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #");
cout<<Color(7,"#");
cout<<Color(8," # ");
cout<<Color(7,"#");
cout<<Color(8,"#");
cout<<Color(8," ##");
cout<<Color(6,"***********");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ## # #");
cout<<Color(6,"***********");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ## # #");
cout<<Color(6,"**********");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," # ");
cout<<Color(7,"#");
cout<<Color(8,"# #");
cout<<Color(6,"**********");
cout<<Color(8,"# ")<<endl;
cout<<Color(8," #### ############ ")<<endl;
PlaySound(TEXT("horse sound.wav"), NULL, SND_SYNC);
return 0;
}
Last edited on
See this:

https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute

This remark seems to address your problem:
Rimmel wrote:
Make sure you restore the original attributes before you write the newline or under some circumstances, you'll get the background color of the *previous* line from the \n until the next line of output.
sorry, what does it mean, i cant understand..
@sharon veronica

I see you changing colors to 4, 6,7 and 8, which are dark red, dark yellow, light gray and dark gray. Where are you using white, as a background or even as a text color? White would be 15.

Here is a small program that lets you change the text color and background colors. Maybe you can use it to do the horse art.
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
102
103
104
// Colored Text on Paper.cpp : main project file.

#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;
				//i++; // Skip to next color
			}
			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;
}


sharon veronica I have also same problem, so i use as following:
if you want to use only one color in whole programme then just use system(); function as i have used the system("color f0") to show the black text color and white background color in under programme.
But if you want to use different colors to lines individually then use Color(); as i have used in under programme.
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
#include<iostream>
#include<windows.h>
void Color(int textColor, int bgColor)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));}
/*
BLACK		 0
BLUE 		 1
GREEN   		 2
CYAN		 3
RED			 4
PURPLE  		 5
YELLOW		 6
LIGHT_GRAY	 7
GRAY		 8
LIGHT_BLUE 	 9
LIGHT_GREEN 	10
LIGHT_CYAN	11
LIGHT_RED	12
PINK			13
MAGNETA		13
LIGHT_YELLOW	14
WHITE		15

*/
void main()
{
system("color f0");
cout<<"welcome"; //this will show the black text and white background.
Color(0,15);
cout<<"This text is in Black Color and has white back ground";
Color(9,14)
cout<<"This text is in Light Blue Color and has Light Yellow Background";
}

Topic archived. No new replies allowed.