Recently there was talk about a program that will create a rainbow effect using html code. So i thought id try making my own. The problem is i keep getting an illegal operation error whenever i run it. I think the problem is in this function....
Code:
void GenerateCode(char *String, char *Buffer)
{
int sLength = strlen(String);
int Total = White - Black;
int IncAmount = Total / sLength;
int Count = 1;
int Color = Black;
while(Count <= sLength)
{
strcat(Buffer, "<FONT COLOR=\"");
strcat(Buffer, (const char*)Color);
strcat(Buffer, "\">");
strcat(Buffer, (const char*)String[Count]);
strcat(Buffer, "</FONT>");
Color = Color + IncAmount;
Count++;
}
return;
}
If you see a problem in there please let me know, i havent used C++ in a while so i might be forgetting something. Ive also included the full source for those who want to see it.
void GenerateCode(char *String, char *Buffer)
{
int sLength = strlen(String);
int Total = White - Black;
int IncAmount = Total / sLength;
int Count = 1;
int Color = Black;
Buffer = new char[29 * sLength + 5];
while(Count <= sLength)
{
strcat(Buffer, "<FONT COLOR=\"");
strcat(Buffer, (const char*)Color);
strcat(Buffer, "\">");
strcat(Buffer, (const char*)String[Count]);
strcat(Buffer, "</FONT>");
Color = Color + IncAmount;
Count++;
};
}
You haven't allocated any memory for Buffer, and you're trying to assign stuff to it.. 29 * sLength is because the length of each <FONT C...></FONT> tag is 29 characters.. and there will be sLength sets of the FONT tag....the + 5 just assures there is enough space in Buffer... you don't need the return; statement either... the function is void. You also need a semicolon after the while-loop.
It's far easier to do this with the STL string class, if you're using C++. Otherwise, if you're using straight C, you'll need to use malloc() or realloc() or somethig like that to allocate the memory.