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:
Here's CString StringIterator::CurrentWord()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::SetCode: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; } }
It compiles fine but crashes. All I need to do is return a new CString that contains the current word. Can anybody help please?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'; }



LinkBack URL
About LinkBacks


