PDA

Click to See Complete Forum and Search --> : A rainbow text color program


ChimpFace9000
Feb 26th, 2001, 09:33 PM
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....


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.

Mar 4th, 2001, 03:32 PM
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.

HarryW
Mar 4th, 2001, 03:41 PM
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.

Mar 4th, 2001, 07:06 PM
Dennis, ya i did, i did it earlier in the code. Its a buffer that can be used at any time.

Mar 4th, 2001, 09:55 PM
Have you tried my code?

I'm just a lil' curious to see if it works.

To use the STL string class just do:


#include <string>
using namespace std;
//.
//..
//...

void GenerateCode(string String, string Buffer)
{
int sLength = String.length();
int Total = White - Black;
int IncAmount = Total / sLength;
int Count = 1;
int Color = Black;
Buffer = "";
while(Count <= sLength)
{
Buffer += "<FONT COLOR=\"";
Buffer += (const char*)Color;
Buffer += "\">";
Buffer += String.substr(Count,1);
Buffer += "</FONT>";
Color = Color + IncAmount;
Count++;
};
}