SIGABRT error in some test cases

Hey, I am trying to solve a problem (Code : AGECAL) in C++ and this is my solution

#include <iostream>
#include <vector>

using namespace std;

class Year {
    int month,mb,db,mc,dc,tot=0,daysInYear;
    long int yb,yc;
    int *array;
    public :
        Year(int mon,int ar[],int yyb,int mmb,int ddb,int yyc,int mmc,int ddc,int sum){
            month = mon;
            array = new int[month];
            array = ar;
            yb=yyb;
            mb=mmb;
            db=ddb;
            yc=yyc;
            mc = mmc;
            dc = ddc;
            daysInYear = sum;
        };

        int calculate();
};


int Year :: calculate(){
    if(yb<yc){
        for(int i = mb-1;i<month;i++){
            if(i == mb-1){
                tot += array[i]-db;
            }else{
                tot += array[i];
            }
            
        }
        if(yb%4 == 0){
            tot +=1;
        }
        yb++;
        tot++;
    }
        while(yb<yc){
            tot += daysInYear;
        if(yb%4 == 0){
            tot +=1;
        }
            yb++;
        }
        if(yb == yc){
            for(int i = mc-1;i>=0;i--){
                if(i==mc-1){
                    tot = tot + dc;
                }else{
                    tot = tot + array[i];
                }
            }
        }
    return tot;
}

int main() {
	int T;
	cin >> T;
	vector<Year> c;
	for(int i =0;i<T;i++){
	    long int yyb,yyc;
	    int mon,mmb,ddb,mmc,ddc,sum=0;
	    int *ar = new int[mon];
	    cin>>mon;
	    for(int i=0;i<mon;i++){
	        cin >> ar[i];
	        sum += ar[i];
	    }
	    cin>>yyb>>mmb>>ddb>>yyc>>mmc>>ddc;
	    c.push_back(Year(mon,ar,yyb,mmb,ddb,yyc,mmc,ddc,sum));
	}
	for(int i=0;i<T;i++){
	   cout<<c[i].calculate()<<endl;
	}
	return 0;
}

It gives me SIGABRT error and i am not sure how to resolve it.
Any help would be much Appreciated

That’s because you declared -

int *ar=new int[mon]

first, and then made the user enter the value of mon.
Interchanging these lines would remove the error.

1 Like