I am trying to convert octal number to decimal number using a string as an input but the answer is ambiguous

  c++, decimal, octal, string

This is the given code. I am accessing each element of the string and trying to multiply it with powers of 8 but, the result is ambiguous.

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

int od(string s)
{
    reverse(s.begin(),s.end());
    
    int ans=0;
    
    for(int i=0;i<s.length();i++)
    {
        ans=ans+s[i]*pow(8,i);
    }
    
    return ans;
}

int main()
{
    string s;
    cin>>s;
    
    cout<<od(s);
    
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT