I just tested it with GCC (g++) on Ubuntu and it is showing the correct result.
Your wrong output indicates that there is probably a rounding error which may be introduced by using the
pow()
function. Depending on your compiler and the
math.h /
cmath header file it may use floating point or a template.
[EDIT]
When using VS there is a
_Pow_int
template used with inline functions in the
math.h header file:
inline double __cdecl pow(int _X, int _Y)
{return (_Pow_int(_X, _Y)); }
So the power is caluclated from integers and the returned result is converted to
double
. This result is then converted to
long long
. These two conversions may lead to rounding errors.
[/EDIT]
But you can avoid using
pow()
by rewriting your function:
long long number=0;
int len = s.size();
for (int i = 0; i < len; i++)
{
int digit=s[i] - '0';
number *= 10;
number += digit;
}
cout<<number<<endl;