Re: Code Help
aaronWbryant@gmail.com wrote:
I need some coding help. It keeps crashing and is not printing
anything to the screen. I have no idea what to fix. Any help is
greatly appreciated.
You need to learn to use a "debugger". It's a program that lets
you execute _your_ program statement by statement, also allowing
you to examine the values of all variables and the call stack.
That's the most important tool in the hands of a programmer.
So far I've only found one place where you make a serious mistake:
[..]
MyString :: MyString()
{
size = 0;
capacity = 1;
Does 'capacity' ever change?
data = new char[capacity];
data[0] = '\0';
}
MyString :: MyString(char * s)
{
size = strlen(s);
capacity = 1;
'1'? Really? That's going to be sufficient?
data = new char[capacity];
while (size >= capacity)
{
grow();
}
strcpy(data, s);
data[size + 1] = '\0';
}
MyString :: MyString (const MyString& s)
{
size = s.size;
capacity = 1;
Again, 1?
data = new char[capacity];
while (size >= capacity)
{
grow();
Ok, let's see the 'grow'...
}
strcpy(data, s.data);
data[size + 1] = '\0';
}
MyString :: ~MyString()
{
delete []data;
}
MyString MyString :: operator =(const MyString& s)
{
delete [] data;
size = s.size;
capacity = 1;
One?
data = new char[capacity];
while (size >= capacity)
{
grow();
}
strcpy(data, s.data);
data[size + 1] = '\0';
return *this;
}
[...]
void MyString :: grow()
{
char *temp;
temp = data;
data = new char[capacity];
OK, so you attempt to allocate another character here.
Why? How does the string *grow*? What should happen
to 'capacity'?
strcpy(data, temp);
data[size] = '\0';
delete [] temp;
}
int MyString :: length () const
{
return size;
}
ostream& operator<< (ostream& out, MyString& m)
{
out << m.data << endl;
return out;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask