what is wrong?

This is a discussion on what is wrong? within the C++ Programming forums, part of the General Programming Boards category; ok so it works fine when its one # Code: #include <iostream.h> #include <string.h> int main() { char story[100]="Bugsy Kludge ...

  1. #1
    Unregistered
    Guest

    what is wrong?

    ok so it works fine when its one #
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {	
    	char story[100]="Bugsy Kludge is a # with our company.";
    	char findnouns[2]="#";
    	char nouns[10]="vegetable";
    	char copystory[100];
    	cout<<story<<endl;
    	int length=strlen(story), length2=strlen(nouns), count=0, count2=0, count3=0;
    	for(count=0;count<length;count++)
    	{
    		if(findnouns[0]==story[count])
    		{
    			strcpy(copystory,story);
    			for(count2=count;count2<(count+length2);count2++)
    			{
    				story[count2]=nouns[count3];
    				count3++;
    			}
    			length=length+length2;
    			story[count+length2]=' ';
    			for(count2=(count+length2+1);count2<length;count2++)
    			{
    				story[count2]=copystory[count2-length2+1];
    			}
    		}
    	}	
    	cout<<story;
    	cout<<endl;
    	return (0);
    }
    so why wont it work when i have 2 #'s?
    can someone help me this is really puzling

  2. #2
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    >> char findnouns[2]="#";

    The size of the string is only 2. Hence it can only hold a single character and an end of string (/0 or NULL)

    Every string must store a NULL character as it's last character...Hence when you want to store a word of 'n' characters (letters), you must initialize a string of 'n+1' characters.
    Last edited by Zeeshan; 06-04-2002 at 12:30 AM.

  3. #3
    Unregistered
    Guest
    nono thats not they way it works i search the array for the # then i replace the # with the word vegetable...

    i do change the size of finnouns i only change the size of story

  4. #4
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I looked over it and I don't really see anything wrong with it. Did it get a compile error? Or did it not run as expected? Did you add the \0 at the end of the array when you were done?

  5. #5
    Unregistered
    Guest
    Try resetting count3 to 0 after it is used to copy nouns into story. If you don't do that, when you find a second # count3 will be of a value that is larger than the largest legal index for vegetable and you will be reading in garbage since you are "overreading" the array holding nouns.

  6. #6
    Unregistered
    Guest
    nice! :P woot
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {	
    	char story[100]="Bugsy Kludge is a # with our company.";
    	char findnouns[2]="#";
    	char nouns[10]="vegetable";
    	char copystory[100];
    	cout<<story<<endl;
    	int length=strlen(story), length2=strlen(nouns), count=0, count2=0, count3=0;
    	for(count=0;count<length;count++)
    	{
    		if(findnouns[0]==story[count])
    		{
    			strcpy(copystory,story);
    			for(count2=count;count2<(count+length2);count2++)
    			{
    				story[count2]=nouns[count3];
    				count3++;
    			}
    			length=length+length2;
    			for(count2=(count+length2);count2<length;count2++)
    			{
    				story[count2]=copystory[count2-length2+1];
    			}
    		}
    		count3=0;
    	}	
    	cout<<story;
    	cout<<endl;
    	return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21