SIGABRT error

Why i am getting SIGABRT error…please help…

#include
using namespace std;

int main() {
int T;
cin>>T;
while(T–)
{
int N,K,dis=0,num; char ch,ch1,ch2;
cin>>N>>K;
string str;
cin>>str;
int ar[K];
for(int i=0;i<K;i++)
cin>>ar[i];
for(int i=0;i<N-1;i++)
{
if(str.at(i)==str.at(i+1))
dis+=2;
else
dis+=1;
}
for(int i=0;i<K;i++)
{ num=ar[i];
if(num!=1 && num!=N)
{
ch=str.at(num-1);
ch1=str.at(num-2);
ch2=str.at(num);
if(ch1!=ch2)
cout<<dis<<endl;
else if(ch==ch1)
{dis-=2;
cout<<dis<<endl;
}
else
{dis+=2;
cout<<dis<<endl;
}
if(str.at(num-1)==‘1’)
str=str.substr(0,num-1)+ “0” + str.substr(num,N);
else
str=str.substr(0,num-1)+ “1” + str.substr(num,N);
}
else if(num==1)
{
ch=str.at(num-1);
ch2=str.at(num);
if(ch==ch2)
{dis-=1;
cout<<dis<<endl;
}
else
{dis+=1;
cout<<dis<<endl;
}

        if(str.at(num-1)=='1')
        str=str.substr(0,num-1)+ "0" + str.substr(num,N);
        else
        str=str.substr(0,num-1)+ "1" + str.substr(num,N);
    }
    else
    {
        ch=str.at(num-1);
        ch1=str.at(num-2);
        if(ch==ch1)
        {dis-=1;
        cout<<dis<<endl;
        }
        else
        {dis+=1;
        cout<<dis<<endl;
        }
        if(str.at(num-1)=='1')
        str=str.substr(0,num-1)+ "0" + str.substr(num,N);
        else
        str=str.substr(0,num-1)+ "1" + str.substr(num,N);
    }
      cout<<str<<endl;
    }
    
}

return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - what Problem are you trying to solve? :slight_smile:

CHARGES…lunchtime may and this is new code

#include
using namespace std;

int main() {
int T;
cin>>T;
while(T–)
{
int N,K,dis=0,num;
cin>>N>>K;
string str;
cin>>str;
for(int i=0;i<N-1;i++)
{
if(str.at(i)==str.at(i+1))
dis+=2;
else
dis+=1;
}
for(int i=0;i<K;i++)
{
cin>>num;
if(num>1 && num<N)
{
if(str.at(num-2)!=str.at(num))
;
else if(str.at(num-1)==str.at(num-2))
dis-=2;
else
dis+=2;
}
if(num==1)
{
if(str.at(num-1)==str.at(num))
dis–;
else
dis++;
}
if(num==N)
{
if(str.at(num-1)==str.at(num-2))
dis–;
else
dis++;
}

    cout<<dis<<endl;
     if(str.at(num-1)=='1')
        str[num-1]='0';
        else
        str[num-1]='1';
    }
    
}

return 0;

}

https://www.codechef.com/viewsolution/47313778

This is the link

Please see now…
I have cut shorted the code as much as possible for me

I think the following is valid test input for this problem:

1
1 1
1
1

If so, it causes an out-of-bounds access with your solution:

[simon@simon-laptop][16:06:15]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling vanshagarwal18-CHARGES.cpp
+ g++ -std=c++14 vanshagarwal18-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][16:08:39]
[~/devel/hackerrank/otherpeoples]>echo "1
1 1
1
1" | ./a.out 
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 1) >= this->size() (which is 1)
Aborted (core dumped)

sorry i did not understand the error