Getting the wrong answer while submitting

problem code TWTCLOSE
#include
#include<string.h>

using namespace std;

int main() {
int n,k,nop=0,x,y;
string str;
int a[1010];
cin>>n>>k;
cin.ignore();

while(k--)
{
    getline(cin,str);
    nop=0;
    
    
    if(str=="CLOSEALL")
    {
        for(int i=0;i<=n;i++)
        a[i]=0;
    }
    
    else
    {
        x=0;
        for(int i=0;i<str.length();i++)
        {
            
            if(str[i]>=48&&str[i]<=60)
            {
                
                y=str[i]-48 ;   
                x=x*10+y;
            }
        }
        a[x]=!a[x];
    }
    for(int i=0;i<=n;i++)
     {   if(a[i]==1)   
        nop++;
        
     }
    cout<<nop<<endl;
}
return 0;

}

A better way to do it would be:

cin >> s;
if (s == "CLOSEALL") {
    memset(a, 0, sizeof a);
}
else {
    cin >> num;
    a[num] = !a[num];
    cout << accumulate(a, a + n, 0) << endl;
}
1 Like

My Solution

ok but why my way is wrong???