Writing a getline function

I am trying to write my own version of the string class and one of my functions is the getline function.

Here is getline:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void MyString::getline( istream& is, char delimit )
{
    int i = 0;
    do
    {
        if( size < capacity )
        {
            data[i] = is.get();
            size++;
            i++;
        }
        else
        {
            capacity *= 2;
            char *temp = new char[capacity];
            for( int i = 0; i < size; i++ )
            {
                temp[i] = data[i];
            }
            delete [] data;
            data = temp;
            
            data[i] = is.get();
            size++;
            i++;
        }
    }while( data[i] != delimit );
}


Here is the MyString class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MyString
{
private:
    int size, capacity;
    char *data;
public:
    MyString( );
    MyString( const char* );
    MyString( const MyString& );
    ~MyString( );
    MyString operator= ( const MyString& );
    MyString operator+ ( const MyString& ) const;
    bool operator== ( const MyString& ) const;
    char& operator[ ] ( int );
    void operator+= ( const MyString& );
    void getline( istream&, char delimit = '\n' );
    int length( ) const;
    friend ostream& operator<< ( ostream&, MyString& );
};


Here is the function call:
1
2
3
fstream f("test.txt", ios::in);
MyString getlineTester;
getlineTester.getline(f);


The function works fine for 13 characters but then stops even if there is more to read. Right now, test.txt has in it, "abcdefghijklmnopqrstuvwxyz" but getline only gets, "abcdefghijklm". What is going on?
1
2
3
4
5
           data[i] = is.get();
            size++;
            i++;
        }
    }while( data[i] != delimit );


What is data[i] when the loop condition is reached?
I added a cout statement and it gave me "45". I assume this to be a trash value. I tried changing my loop condition to data[i-1] but then it acted like an infinite loop. Then I figured out that the problem was in my input file. I forgot to add the newline on the end. *facepalm*. The loop condition still needs to be data[i-1] though. Thank you for your help!
Topic archived. No new replies allowed.