Help me in solving BITMEDU6 problem

My issue

what is error in my code

My code

#include <bits/stdc++.h>
#include<string>
using namespace std;

int main() {
	int n;
	cin>>n;
	string str=" ";
    while(n>0)
    {
        if(n%2==0)str+='0';
        else
        str+='1';
        n=n/2;
    }
    for(int i=str.length();i>0;i--)
   { cout<<str[i];
   }
    cout<<endl;
}

Learning course: Bit manipulation
Problem Link: Decimal to binary Practice Problem in Bit manipulation - CodeChef

A C/C++ string always ends with a number 0

Say:
std::string example = “hello”;

The string is actually an array like this:

“h”, “e”, “l”, “l”, “o”, 0

There are 5 characters, yes, but your code tries to print the last 0 on that “i = string.size()”.