can anyone tell me why my code would output the string designated plus ten or more characters of non sense at the end of it? my code also has a header with #include statements and a main that calls the functions.
#include "String.h"
//Default constructor
String::String()
{
Text = NULL;
}
//Init constructor, allocates string size characters long
String::String(int size)
{
Text = new char[size];
}
// Initializes this String with a specified C-string
String::String (char* source)
{
Text = NULL;
*this = source;
}
// Perform deep copy!
String& String::operator = (char* source)
{
//0. Delete old Text
delete [] Text;
//1. Figure out source size
int lengthOfSource = GetLength(source);
//2. Allocate Text, Size +1 to account for a NULL terminator
Text = new char[lengthOfSource];
//3. Copy
for ( int i = 0; i < lengthOfSource; i++)
Text[i] = source[i];
return *this;
}
//Copy constructor, performs deep-copy
String::String(const String& source)
{
Text = NULL;
*this = source;
}
//Assignment operator, performs deep-copy
String& String::operator = (const String& source)
{
return (*this=source.GetText());
}
//Destructor disposes allocated string
String::~String()
{
delete[] Text;
}
// Index operator, returns a reference to a character
//throws an exception when index is out of bounds
char& String::operator [] (int index) const
{
if ( (index < 0) || (index >= GetLength(Text)) )
{
stringstream error;
error << "Exception occurred: String::operator[] index " << index << " is out of bounds (0.." << (GetLength(Text) - 1) << ")";
//throws exception
throw error.str();
}
return Text[index-1];
}
//Concatenation operator, returns a new string
String String::operator + (const String& appendix) const
{
String newText = String(Text);
String newString = GetLength(Text) + appendix.GetLength();
int j = 0;
for (int i = 0; i < GetLength(Text); i++)
{
newString[i] = Text[i];
j++;
}
for (int i = 0; i < appendix.GetLength(); i++)
newString[i+j]= appendix[i];
return newString;
}
//Comparison operators
bool String::operator == (const String& compareTo) const
{
for ( int i = 0; i < compareTo.GetLength(); i++ )
{
if (Text[i] != compareTo[i])
return true;
}
return false;
}
bool String::operator != (const String& compareTo) const
{
return !(*this == compareTo);
}
/*static*/int String::GetLength(char* text)
{
int i = 0;
while ( text[i] != NULL )
i++;
return i;
}
// Returns the length of this String
int String::GetLength() const
{
return GetLength(Text);
}
char* String::GetText() const
{
return Text;
}
//Returns an index of aChar in this String starting the search
// from the startposition; if the character is not found returns NOT_FOUND
int String::Find(char aChar, int startPosition /*= 0*/) const
{
for ( int i = startPosition; i < GetLength(Text); i++)
{
if (Text[i] == aChar)
return i+1;
}
return NOT_FOUND;
}
//Returns a substring of this String starting from startPosition and
//length characters long (if length = 0 then substring spans
// from startPosition until the end of this String)
String String::Substring(int startPosition, int length/* = 0*/) const
{
String newString;
int cnt = 0;
int size = length;
while (size>=0)
{
newString[cnt]=Text[startPosition];
cnt++;
size--;
startPosition++;
}
return newString;
}
// Stream output operator for printing String to console
std::ostream& operator << (std::ostream& out, const String& myString)
{
return out << myString.GetText();
}