Im having a lot of trouble figuring out whta's wrong in my code...
it compiles and everything but every time I try to run the program it crashes before even starting and displays the following message:
Debug Assertion Failed!
Program: c:\CIS 278\MyString\Debug\MyString.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information..........
I'm trying to finish this school project before the end of today, so please any help will be greatly appreciated.
Here's a copy of my Code:
//Project 6 by Juan Hernandez
//CIS 278
// This is the MyString.cpp file
#include <iostream>
#include "MyString.h"
using namespace std;
MyString::MyString(){
str = new char[9];
str ="Default";
length = strlen(str);
}
MyString::MyString(char newstr[]){
length = strlen(newstr);
delete [] str;
str = new char[length];
for (int i = 0; i < length ; i++)
str[i] = newstr[i];
}
MyString::MyString(const MyString& obj) : length(obj.getLen()){
delete [] str;
str = new char[length];
for(int i = 0; i < obj.getLen() ; i++)
str[i] = obj.getElement(i);
}
MyString::~MyString(){
delete [] str;
}
int MyString:: getLen() const{
return strlen(str);
}
void MyString::setElement(int index, char item){
str[index] = item;
}
char MyString::getElement(int index) const{
return str[index];
}
bool MyString::operator==(const MyString& obj){
bool answer = true;
if(length != obj.getLen())
answer = false;
do{
for(int i = 0; i < obj.getLen() ; i++)
if ( str[i] != obj.getElement(i))
answer = false;
}while(answer == true);
return answer;
}
bool MyString::operator !=(const MyString& obj){
bool answer = false;
if(length != obj.getLen())
answer = true;
do{
for(int i = 0; i < obj.getLen() ; i++)
if ( str[i] != obj.getElement(i))
answer = true;
}while(answer == false);
return answer;
}
char& MyString::operator [](int in){
if((in < 0) || (in > length)){
cout << "Error, contact software manufacturer for more info." << endl;
exit(1);}
else
return str[in];
}
MyString& MyString::operator=(const MyString& obj){
if(this == &obj){
return *this;
}
else{
length = obj.getLen();
delete [] str;
str = new char[length];
for(int i = 0 ; i < length ; i++)
str[i] = obj.getElement(i);
return *this;
}
}
ostream& operator<<(ostream& cout, const MyString& obj){
cout << obj.str;
return cout;
}