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;
}

Your pointer to dynamic memory was corrupted somehow or was otherwise invalid. Usually this is the result of writing outside the boundaries of the requested memory because those areas are often used by the memory manager to store bookkeeping information, and thus they're very fragile.

The following stands out as both a memory leak and a likely disconnect because your destructor doesn't differentiate between a string literal (which cannot be deleted) and the result of new[] (which can be deleted).

str = new char[9];
str ="Default";

The non-default constructors also have an issue because they try to delete str before doing anything else. However, unless str is a valid pointer returned by new[] or a null pointer, calling delete[] is undefined behavior. Since the object is completely new at this point, trying to delete existing memory is nonsensical.

MyString::MyString(char newstr[]){
    length = strlen(newstr);
    delete [] str;
    str = new char[length];

I suspect both of these issues work together to corrupt your memory and throw errors, so if you fix them, the class will probably work more to your liking. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.