Conversion of string to number using recursion

here is my code :
#include
#include
#include <bits/stdc++.h>
using namespace std;

long long int string2number(string s , int i , int length ){

if(s.at(i+1)=='\n'){

    return s.at(i)-'0' ;
}

 long long int ans= (s.at(i)-'0')*pow(10, length-(i+1) ) + string2number(s,i+1 , length);

return ans;

}
int main() {

   string s;
   cin>>s;
   cout<<s;
   cout<<string2number( s,0 ,s.length() );



return 0;

}
am getting this error ( for input 1234 ):
terminate called after throwing an instance of ‘std::out_of_range’
what(): basic_string::at: __n (which is 4) >= this->size() (which is 4)

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

long long int string2number(string s , int i , int length ){

if(s[i+1] =='\0'){

    return s[i] -'0' ;
}

 long long int ans= (s[i]-'0')*pow(10, length-(i+1) ) + string2number(s,i+1 , length);

return ans;

}
int main() {

   string s;
   cin>>s;
   cout<<s;
   cout<<string2number( s,0 ,s.length() );



return 0;

}```


I have implement the same logic just by changing s.at(i) to s[i] and making that '\n' to '\0' and errors are remove you just to correct your logic

So you are facing this error because s.at() cannot access your ‘/0’. so you need to use s[i] to check ‘/0’.
Thank you