-1

Here is my code and is throwing me an assertion error. I really don't know what this means. This is the first time I have encountered an issue like this.

#include <iostream>
#include "string.h"
using namespace std;

class myString {
private:
   // these functions are not
   // needed outside the class
   void allocate(int);
   void copy(char *, char *);
   char *string;
public:   
   // member functions
   myString();
   myString(char *);
   int getLength();
   int getLength(char *);
   void empty();
   bool isEmpty();
   void putString(char *);
   char *getString();
   char *fromLeft(int);
   char *fromRight(int);
   char *fromMid(int, int);
   ~myString();
};

//--------------------------------
//------------MEMBER--------------
//---FUNCTION DEFINITION STARTS---
void myString::allocate(int size)
    { empty();
      string=new char[size];
      if(string==NULL) exit(0); }

void myString::copy(char *str1, char *str2)
   { int length=getLength(str2);
    for(int i=0;i<=length;i++)
      str1[i]=str2[i]; }

   void myString::empty()
   {
    if(string!=NULL)
      {
       delete []string;
       string=NULL;
      }
   }

   bool myString::isEmpty()
   {
    if(string!=NULL)
       return false;

    return true;
   }

  int myString::getLength(char *str)
  {
   int i=0;

   while(str[i]!='\0')
     i++;

   return i;
  }

  int myString::getLength()
  {
   if(string!=NULL)
     return getLength(string);

   return -1;
  }

  myString::myString(char *str)
  {
   string=NULL;
   int size=getLength(str)+1;

   allocate(size);

   copy(string,str);
  }

  myString::myString()
  {
   string=NULL;
  }

  void myString::putString(char *str)
  {
   int size=getLength(str)+1;

   allocate(size);

   copy(string,str);
  }

  char *myString::getString()
  {
   if(string!=NULL)
     {
      return string;
     } else return NULL;
  }

  myString::~myString()
  {
   if(string!=NULL)
     delete []string;
  }

  char *myString::fromLeft(int chr)
  {
   if(string!=NULL)
     {
      char *temp;
      temp=new char[chr+1];// account for NULL character
    int i;
      if(temp==NULL) exit(1);// can't get memory

      for( i=0;i<chr;i++)
        temp[i]=string[i]; // copy chr number of characters
        temp[i]='\0';  // terminate it
      return temp;
     }else return NULL;
  }

  char *myString::fromRight(int chr)
  {
   if(string!=NULL)
     {
      char *temp;
      temp=new char[chr+2];
      int a=0;

      if(temp==NULL) exit(1);

      int i=getLength()-1;
      int j=(i-chr)+1;

      for(j;j<=i;j++)
        {
         temp[a]=string[j];
         a++;
        }
   temp[a]='\0';
   return temp;
     }
else return NULL;  }

  char *myString::fromMid(int a,int b)
  {
   if(string!=NULL)
     {
      int size=b+1;
      char *temp;
      temp=new char[size];
      int i=a,j=0,k=(a+b);

      for(i;i<k;i++)
        {
         temp[j]=string[i];
         j++;
        }
   temp[j]='\0';
   return temp;
     }
else return NULL;  }
   //-----------MEMBER-------------
   //---FUNCTION DEFINITION ENDS---
   //------------------------------

class Month {
private: 
    myString monthName;
    int monthNumber;
public:
    Month() 
        { monthNumber=1; monthName.putString("January"); }
    Month(char *s) 
        { 
            monthName.putString(s);
            if (strcmp(s,"January")==0)  monthNumber=1; 
            if (strcmp(s,"February")==0) monthNumber=2;
            if (strcmp(s,"March")==0) monthNumber=3;
            if (strcmp(s,"April")==0) monthNumber=4;
            if (strcmp(s,"May")==0) monthNumber=5;
            if (strcmp(s,"June")==0) monthNumber=6;
            if (strcmp(s,"July")==0) monthNumber=7;
            if (strcmp(s,"August")==0) monthNumber=8;
            if (strcmp(s,"September")==0) monthNumber=9;
            if (strcmp(s,"October")==0) monthNumber=10;
            if (strcmp(s,"November")==0) monthNumber=11;
            if (strcmp(s,"Decevember")==0) monthNumber=12;
        }
    Month(int n)        {
        monthNumber=n;
        if (monthNumber==1) monthName.putString("January");
        if (monthNumber==2) monthName.putString("February");
        if (monthNumber==3) monthName.putString("March");
        if (monthNumber==4) monthName.putString("April");
        if (monthNumber==5) monthName.putString("May");
        if (monthNumber==6) monthName.putString("June");    
        if (monthNumber==7) monthName.putString("July");
        if (monthNumber==8) monthName.putString("August");
        if (monthNumber==9) monthName.putString("September");
        if (monthNumber==10) monthName.putString("October");
        if (monthNumber==11) monthName.putString("November");
        if (monthNumber==12) monthName.putString("December");
    }
    void SetMonthNumber(int n) { monthNumber=n; 

        if (monthNumber==1) monthName.putString("January");
        if (monthNumber==2) monthName.putString("February");
        if (monthNumber==3) monthName.putString("March");
        if (monthNumber==4) monthName.putString("April");
        if (monthNumber==5) monthName.putString("May");
        if (monthNumber==6) monthName.putString("June");
        if (monthNumber==7) monthName.putString("July");
        if (monthNumber==8) monthName.putString("August");
        if (monthNumber==9) monthName.putString("September");
        if (monthNumber==10) monthName.putString("October");
        if (monthNumber==11) monthName.putString("November");
        if (monthNumber==12) monthName.putString("December");  
    }
    void SetMonthName(char *s) { monthName.putString(s);

        if (strcmp(s, "January")==0) monthNumber=1;
        if (strcmp(s, "February")==0) monthNumber=2;
        if (strcmp(s, "March")==0) monthNumber=3;
        if (strcmp(s, "April")==0) monthNumber=4;
        if (strcmp(s, "May")==0) monthNumber=5;
        if (strcmp(s, "June")==0) monthNumber=6;
        if (strcmp(s, "July")==0) monthNumber=7;
        if (strcmp(s, "August")==0) monthNumber=8;
        if (strcmp(s, "September")==0) monthNumber=9;
        if (strcmp(s, "October")==0) monthNumber=10;
        if (strcmp(s, "November")==0) monthNumber=11;
        if (strcmp(s, "December")==0) monthNumber=12; 
    }        
    int GetMonthNumber() const 
        {return monthNumber;}
    myString GetMonthName() const 
        { return monthName; } 
    Month & operator ++()         {  // prefix
        monthNumber++; 
        SetMonthNumber(monthNumber); 
        return *this;
    }
    Month  operator++(int){
        Month Temp; 
        Temp=*this; 
        monthNumber++;
        SetMonthNumber(monthNumber);
        return Temp;
    }// Postfix 

    Month & operator --() {
        monthNumber--;
        SetMonthNumber(monthNumber);
        return *this;
    }
    Month operator--(int) {
        Month Temp;
        Temp=*this;
        monthNumber--;
        return Temp;
    }    
    friend ostream & operator<<(ostream & O, Month a);
    //friend istream & operator>>(istream & I, Month a);
};

ostream & operator<<(ostream & O, Month a) {
    O << a.monthNumber << " " << a.monthName.getString();
        return O; 
    }

istream &operator >> (istream &is, Month &a)    //cin operator overload
    {
        int num;
        char name[12];
        is >> num;
        is >> name;

        a.SetMonthNumber(num);
        a.SetMonthName(name);
        return is;
    }



int main()
{   
    Month M;        
    char name[12];
    int num;
    cout << M << endl;
    M++;
    cout << M << endl;   
    M.SetMonthNumber(5);
    cout << M << endl;    
    cout << "Enter a month name  ex: January" << endl;
    cin >> name;
    M.SetMonthName(name);
    cout << M << endl;
    cout << "Enter a month number (1-12)" << endl;
    cin >>  num;
    M.SetMonthNumber(num);
    cout << M << endl;   
    cout << " Enter a month number and name  ex: 2 February  or  3 March" << endl;
    cin >> M;
    cout << M << endl;
    int pause;
    cin >> pause;
    return 0;
}
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Please form an [MCVE](http://stackoverflow.com/help/mcve) (debugging it will instantly tell you which line of your code triggers the assertion) and then look at the related questions on the site to see if your fundamental problem is the same. There are lots of questions with this error, so you might find something when your code isn't 90% proprietary. – chris Mar 26 '15 at 14:46
  • 1
    You probably forgot to make a copy constructor, leading to double-freeing of memory. But more importantly, it looks like you wrote way too much code before you started testing your string class. – molbdnilo Mar 26 '15 at 14:52

1 Answers1

0

You will have to remove the delete from the method empty(). delete should be part of the destructor. Please see the new definition

void myString::empty()

{

if(string!=NULL)

  {

   string=NULL;

  }

}

balumohan
  • 51
  • 1
  • 6