using string in SetCons...

I have this code:

void text_color(string color1, string color2, string color3)
{
string colortmp, colortmp1;
if((color2 == "NULL") && (color3 == "NULL"))
{
colortmp = "FOREGROUND_" + color1;
SetConsoleTextAttribute(h, colortmp.c_str() | FOREGROUND_INTENSITY); // Error line
}
else if(color3 == "NULL")
{
colortmp = "FOREGROUND_" + color1;
colortmp1 = "FOREGROUND_" + color2;
SetConsoleTextAttribute(h, colortmp.c_str() | colortmp1.c_str() | FOREGROUND_INTENSITY); // Error line
}
else if(color2 == "NULL")
{
Sleep(1);
}
else if(color1 == "NULL")
{
SetConsoleTextAttribute ( h, wOldColorAttrs);
}
else
{
cout << endl << "error to load paint" << endl;
}
}

And error is:
invalid operands of types `const char*' and `int' to binary `operator|'
invalid operands of types `const char*' and `const char*' to binary `operator|'

please help
Last edited on
You don't need to include the text that you are trying to modify(what gave you the idea to OR it?). Here is the manual page describing the "SetConsoleTextAttribute()": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx

You'll notice that it is a global setting for the console, not a setting per line of text.
I tryed set color of one for line text. But I could not run program. I don't know how I will continue. Can you help me to do differently?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <windows.h>

using namespace std;

int main() 
{
  HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hConsoleOut, FOREGROUND_RED);
  cout << "Hello World!\n\n";
  SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN);
  cout << "Hello World!\n\n";
  system("pause");
  return 0;
}


Here is a list of color codes:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes
Last edited on
I know this method but whether it can be done even with variables of type String
Windows accepts only a WORD as color. Of course you can write your own code to convert a string to a WORD like:

1
2
3
4
WORD GetColor(string colorName)
{ 
   // Your code here
}
Please. Can you show me extended code or show us an example? Because I don't know quite how to use it.
Last edited on
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
#include <iostream>
#include <string>
#include <windows.h>
#include <ctype.h>

using namespace std;

WORD StringToColor(string colorName)
{ 
  const int FOREGROUND_WHITE = 7;

  for (size_t i = 0; i < colorName.size(); i++)
  {
    colorName[i] = toupper(colorName[i]);
  }

  if (colorName == "RED")
    return FOREGROUND_RED;
  else if (colorName == "BLUE")
    return FOREGROUND_RED;
  else if (colorName == "GREEN")
    return FOREGROUND_GREEN;
  else 
    return FOREGROUND_WHITE;
}

int main() 
{
 
  HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hConsoleOut, StringToColor("RED"));
  cout << "Hello World!\n\n";
  SetConsoleTextAttribute(hConsoleOut, StringToColor("GREEN"));
  cout << "Hello World!\n\n";
  system("pause");
  return 0;
}

Thank you very very very much. :)
Topic archived. No new replies allowed.