Thread: help me out with this bug

  1. #1
    Unregistered
    Guest

    help me out with this bug

    for some reason i just cant seem to be able to see th4e bug that screws up my replacement of words

    heres the code
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    int main()
    {
    	char FileContents[500]="three blind mice three blind mice three blind mice", WordToDelete[10]="three", WordToInsert[10]="weirdos";
    	char StoreFileContents[500]="three blind mice three blind mice three blind mice";
    	int length1=strlen(FileContents);
    	int length2=strlen(WordToDelete);
    	int length3=strlen(WordToInsert);
    	int count,count2,count3;
    	cout<<FileContents<<endl;
    	for(count=0;count<=length1;count++)
    	{		
    		if(FileContents[count]==WordToDelete[0])
    		{
    			for(count2=count;count2<=(count+length2);count2++)
    			{
    				if(count2==(count+length2))
    					if(length2==length3)
    					{
    						for(count3=count;count3<(count+length2);count3++)
    						{
    							FileContents[count3]=WordToInsert[count3-count];
    						}
    					}
    					else if(length2<length3)
    					{
    						//count is location of first letter of the word to remove
    						for(count3=count;count3<=(count+length3);count3++)
    							FileContents[count3]=WordToInsert[count3-count];
    						for(count3=count;count3<=length1;count3++)
    							FileContents[count3+length3+(length3-length2)]=StoreFileContents[count3+length2];						
    					}
    			}
    		}
    	}
    	cout<<endl;
    	for(count=0;count<=length1;count++)
    		cout<<FileContents[count];
    	cout<<endl;
    	cout<<FileContents[20];
    	return (0);
    }

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i just skimed you code, but it looks like you're not making up for the difference in word lenths between three and weridos. i think you have to push the entire array back one char for each replacement yiou try. or you can just make the replacement the same length as the replaced. good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zooming Algorithm Bug
    By stuffy_boiz in forum C Programming
    Replies: 2
    Last Post: 01-19-2009, 02:34 AM
  2. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  3. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  4. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  5. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM