Not able to pass test case 3 < String Compression >

#include

using namespace std;

void stringCompression(string S,int length)
{
int count =1;

cout<<S[0];

if(length==1)
{
    cout<<count;
}

for (int i = 1; i < length; ++i)
{
    if(S[i]==S[i-1])
    {
        count++;
    }
    else
    {
        cout<<count;
        cout<<S[i];
        count=1;
    }
}

cout<<count<<endl;  

}

int main()
{
string S;
cin>>S;

int length= S.length();

stringCompression(S,length);

}

Hello @rprahulpal03 i have corrected your code and now it is passsing every test case .
i have commented the mistake:


if you have any further doubt you can ask here:
Happy Learning!!