Thread: Please help with String class, returning strings

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    37

    Please help with String class, returning strings

    I'm desperate here, please be gracious. I just don't know what to do with this CStringIterator class function that returns the current word in a string.

    Here's main:
    Code:
    void main()
    {
    	int integer;
    	char* bob = "Bob";
    	char* tim = "Tim";
    
    	CString tester ("Tyson Lee Kirksey");
    	CString tester2;
    	
    	CStringIterator Iter (tester);
    
    	cout << Iter.CurrentCharacter()<< endl;
    
    	Iter.MoveForward();
    	Iter.MoveForward();
    	Iter.MoveForward();
    	Iter.MoveForward();
    	Iter.MoveForward();
    
    	cout << Iter.CurrentCharacter()<< endl;
    
    	//Iter.MoveToFront();
    
    	tester2.Set (Iter.CurrentWord());
    	CStringIterator Iter2 (tester2);
    
    	cout << Iter2.CurrentCharacter();
    Here's CString StringIterator::CurrentWord()
    Code:
    CString CStringIterator::CurrentWord ()
    {
    	//CString return_me;
    	//return_me = new CString;
    
    	char* NewString = new char[40];
    	//return_me = CString(NewString);
    
    	int first;
    	int last;
    	int length = m_String.Length() - 1;
    	int count1 = 0;
    	int count2 = 0;
    	int total;
    
    	if (AtWhiteSpace())
    	{
    		//return_me.Set(" ");
    		//return_me.pString[1] = '\0';
    		NewString[0] = ' ';
    		NewString[1] = '\0';
    		return NewString;
    	}
    	else
    	{
    		do
    		{
    			MoveForward();
    			count1++;
    		}
    		while (!AtWhiteSpace() && (m_Index + count1) != length);
    		if (AtWhiteSpace())
    			last = m_Index - 1;
    		else					// The last point is now known
    			last = m_Index;
    		
    		m_Index = m_Index - count1;
    
    		do
    		{
    			MovePrevious();
    			count2++;
    		}
    		while (!AtWhiteSpace() && m_Index != 0);
    
    		if (AtWhiteSpace())
    			first = m_Index + 1;
    		else
    			first = 0;
    
    		m_Index = m_Index + count2;
    
    		//Create the new String
    
    		total = last - first;
    		for (int count3 = 0; count3 <= total; count3++)
    		{
    //			return_me.pString[count3] = m_String.pString[first];
    			first++;
    		}
    	    
    		//add null terminator
    //		return_me.pString[total + 1] = '\0';
    	//	return return_me;
    	}
    }
    Here's CString::Set
    Code:
    void CString::Set(CString NewString)
    {
    	//char* NewString = new char[newStringLength];
    	char* pString = new char[NewString.Length()+1];
    	strcpy(pString,NewString.pString);
    
    	//add the NULL value on end
    	pString[strlen(pString)] = '\0';
    }
    It compiles fine but crashes. All I need to do is return a new CString that contains the current word. Can anybody help please?

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Is this CString class your own or the one from MFC? Next with out all the code it would not be very easy to see exactly what your doing. What is the crash that occurs? To return the CString you could simply call the Contructor that takes a char* as an argument like so:
    Code:
    return CString(NewString);
    That will create a unnamed temporary object.
    Last edited by velius; 09-24-2003 at 05:14 AM.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Its my own class, here's how it looks:

    Code:
    class CString
    {
    	
    	friend class CStringIterator;
    
       public:
    	   CString(); //works
    	   CString(CString &string); //works
    	   CString(char*); //works
    	   ~CString(); //works
    	   //void Set(char* NewString);
    	   void Set(CString NewString);
    	   bool IsEmpty(); //works
    	   int Length(); //works
    	   //void Append(char* NewString); //works but don't need
    	   void Append(CString &NewString); //works
    	   //bool Compare(char* NewString); 
    	   bool Compare(CString NewString);//works
    	   void Empty(); //works   
    	   void Display(); //works
    	   void Cool_Function(); //works
    
       private:
    	   char* pString;
    
    };
    Stepping thru it w/ the debugger, when I'm finished setting tester2 to Iter.CurrentWord, tester2's value says "Bad PTR". How come?

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    I will need to see all of the code in order to fully understand what you're doing. Please zip it and send it to me. Then I can help you out.
    Last edited by velius; 09-24-2003 at 10:06 AM.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Ah, forget about it. I got it working. Thanks for offering though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM