Results 1 to 5 of 5

Thread: A rainbow text color program

  1. #1
    ChimpFace9000
    Guest

    Post

    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.
    Attached Files Attached Files

  2. #2
    Guest
    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;
              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.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Guest
    Dennis, ya i did, i did it earlier in the code. Its a buffer that can be used at any time.

  5. #5
    Guest
    Have you tried my code?

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

    To use the STL string class just do:

    Code:
    #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++;
    	};
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width