U
U
UrbanRider2011-04-18 12:55:14
Programming
UrbanRider, 2011-04-18 12:55:14

Deriving an array of characters cout from another class structure

It will probably be more difficult to explain in words, I immediately show the code:

ConsoleFitches class file
#include #include <string.h>
using namespace std;
class ConsoleFitches
{
public:
void SetAttDefault() const {char att[]="\033[0m";cout << att;};
void SetAttBold() const {char att[]="\033[1m";cout << att;};
void SetAttBlink() const {char att[]="\033[5m";cout << att;};
void SetTextColor(char color[5]) const{cout<<color;};
void SetBgColor(char* color[5]) const{cout<<color;};
ConsoleFitches();
struct Color
{
char Black[5];
char Red[5];
char Green[5];
char Brown[5];
char Blue[5];
char Violet[5];
char Cyan[5];
char Gray[5];
};
Color TextColor,BgColor;
};
ConsoleFitches::ConsoleFitches()
{
Color TextColor,BgColor;
strcpy(TextColor.Black,"\033[30m");
strcpy(TextColor.Red,"\033[31m");
strcpy(TextColor.Green,"\033[32m");
strcpy(TextColor.Brown,"\033[33m");
strcpy(TextColor.Blue,"\033[34m");
strcpy(TextColor.Violet,"\033[35m");
strcpy(TextColor.Cyan,"\033[36m");
strcpy(TextColor.Gray,"\033[37m");
strcpy(BgColor.Black,"\033[40m");
strcpy(BgColor.Red,"\033[41m");
strcpy(BgColor.Green,"\033[42m");
strcpy(BgColor.Brown,"\033[43m");
strcpy(BgColor.Blue,"\033[44m");
strcpy(BgColor.Violet,"\033[45m");
strcpy(BgColor.Cyan,"\033[46m");
strcpy(BgColor.Gray,"\033[47m");
};


Основной файл программы:
#include #include "ConsoleFitches.cpp"
using namespace std;
int main()
{
ConsoleFitches cf;
cf.SetTextColor(cf.TextColor.Red);;
cout<<"HELLO";
cf.SetAttDefault();
return 0;
}


Результатом программы должна быть надпись HELLO красного цвета. Вместо этого выводится кракозябр+HELLO.

Если же сделать напрямую cout<<"\033[31m", а за ним вывести текст, то все нормально.

Подскажите где я туплю, пожалуйста.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mark_ablov, 2011-04-18
@mark_ablov

the code is terrible :)
to solve the problem - expand the buffers to 6 characters, because you still need a terminating zero

U
umcherrel, 2011-04-18
@umcherrel

You created variables in the constructor, this is clearly a bug.

U
UrbanRider, 2011-04-18
@UrbanRider

Thank you so much, you were both right. It was necessary to increase the buffer to 6 characters, as well as create instances of the structure in the class declaration, and fill them in the constructor.
The working code now looks like this:
#include #include <string.h>
using namespace std;
class ConsoleFitches
{
public:
void SetAttDefault() const {char att[]="\033[0m";cout << att;};
void SetAttBold() const {char att[]="\033[1m";cout << att;};
void SetAttBlink() const {char att[]="\033[5m";cout << att;};
void SetTextColor(char color[5]) const{cout<<color;};
void SetBgColor(char* color[5]) const{cout<<color;};
ConsoleFitches();
struct Color
{
char Black[6];
char Red[6];
char Green[6];
char Brown[6];
char Blue[6];
char Violet[6];
char Cyan[6];
char Gray[6];
};
Color TextColor,BgColor;
};
ConsoleFitches::ConsoleFitches()
{
strcpy(TextColor.Black,"\033[30");
strcpy(TextColor.Red,"\033[31m");
strcpy(TextColor.Green,"\033[32");
strcpy(TextColor.Brown,"\033[33");
strcpy(TextColor.Blue,"\033[34");
strcpy(TextColor.Violet,"\033[35");
strcpy(TextColor.Cyan,"\033[36");
strcpy(TextColor.Gray,"\033[37");
strcpy(BgColor.Black,"\033[40");
strcpy(BgColor.Red,"\033[41");
strcpy(BgColor.Green,"\033[42");
strcpy(BgColor.Brown,"\033[43");
strcpy(BgColor.Blue,"\033[44");
strcpy(BgColor.Violet,"\033[45");
strcpy(BgColor.Cyan,"\033[46");
strcpy(BgColor.Gray,"\033[47");
};
Также хотелось бы получить уточнение, чем этот код ужасен, и как написать правильно?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question