Code Help
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.
Here is my driver:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "MyString.h"
using namespace std;
int main()
{
MyString fname;
MyString lname("Smith");
MyString mname(lname);
MyString line, paper;
MyString fullName;
ifstream inData;
fname = "Bob";
fname = fname;
cout << "first name: " << fname << endl;
cout <<fname<<" "<<mname<<". "<<lname<<endl;
inData.open("paper.txt");
if(!inData)
{
cerr <<"Error: File NOT open\n";
exit(0);
}
line.getline(inData);
while(!inData.eof())
{
line += "\n";
paper += line;
line.getline(inData);
}
line += "\n";
paper += line;
cout <<"The paper has a length of "<<paper.length()<<".\n";
cout << line;
cout << "\n\n\n";
cout << paper;
cout << "\n\n\n";
//Test the = operator
mname = "A";
fname = fname + " " + mname + ". " + lname;
fullName = fname;
cout << "Fill Name is : "<<fullName<<endl;
if(fname == fullName)
cout<<"But the names remain the same\n";
else
cout<<"Why did you change your name?\n";
fname = "Mary";
if(fname == fullName)
cout<<"But the names remain the same\n";
else
cout<<"Why did you change your name?\n";
for(int index = 0; index < fullName.length(); index++)
cout << fullName[index] << endl;
return 0;
}
Here is my header file:
#ifndef _MYSTRING
#define _MYSTRING
#include <iostream>
using namespace std;
class MyString
{
private:
int size;
int capacity;
char *data;
public:
MyString();
MyString(char *);
MyString (const MyString&);
~MyString();
MyString operator =(const MyString &);
MyString& append (const MyString&);
MyString& erase();
MyString operator + (const MyString&) const;
bool operator == (const MyString&);
bool operator < (const MyString&);
bool operator > (const MyString&);
bool operator <= (const MyString&);
bool operator >= (const MyString&);
bool operator != (const MyString&);
void operator += (const MyString&);
char& operator [] (int);
void getline (istream&);
int length () const;
friend ostream& operator<< (ostream&, MyString&);
void grow();
};
#endif
Here are my functions:
#include "MyString.h"
#include <iostream>
#include <cstdlib>
using namespace std;
MyString :: MyString()
{
size = 0;
capacity = 1;
data = new char[capacity];
data[0] = '\0';
}
MyString :: MyString(char * s)
{
size = strlen(s);
capacity = 1;
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;
data = new char[capacity];
while (size >= capacity)
{
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;
data = new char[capacity];
while (size >= capacity)
{
grow();
}
strcpy(data, s.data);
data[size + 1] = '\0';
return *this;
}
MyString& MyString :: append (const MyString& s)
{
size += s.size;
while(size >= capacity)
{
grow();
}
strcat(data, s.data);
data[size + 1] = '\0';
return *this;
}
MyString& MyString :: erase()
{
size = 0;
data[0] = '\0';
return *this;
}
MyString MyString :: operator + (const MyString& s) const
{
char *addition;
addition = new char[strlen(data) + strlen(s.data)];
strcpy(addition, strcat(data, s.data));
addition[size] = '\0';
return addition;
}
bool MyString :: operator == (const MyString& s)
{
if (strcmp(data, s.data) == 0)
{
return true;
}
else
{
return false;
}
}
bool MyString :: operator < (const MyString& s)
{
if (strcmp(data, s.data) < 0)
{
return true;
}
else
{
return false;
}
}
bool MyString :: operator > (const MyString& s)
{
if (strcmp(data , s.data) > 0)
{
return true;
}
else
{
return false;
}
}
bool MyString :: operator <= (const MyString& s)
{
if (strcmp(data, s.data) <= 0)
{
return true;
}
else
{
return false;
}
}
bool MyString :: operator >= (const MyString& s)
{
if (strcmp(data, s.data) >= 0)
{
return true;
}
else
{
return false;
}
}
bool MyString :: operator != (const MyString& s)
{
if (!strcmp(data, s.data))
{
return true;
}
else
{
return false;
}
}
void MyString :: operator += (const MyString& s)
{
size += s.size;
capacity = 1;
while(size >= capacity)
{
grow();
}
strcat(data, s.data);
data[size] = '\0';
}
char& MyString :: operator [] (int n)
{
return data[n];
}
void MyString :: getline (istream& in)
{
size = 0;
char c;
in.get(c);
while(c != '\n' && in)
{
data[size] = c;
size++;
if (size >= capacity)
{
grow();
}
in.get(c);
}
data[size] = '\0';
}
void MyString :: grow()
{
char *temp;
temp = data;
data = new char[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;
}