So, as part of an assignment, I wrote a program to convert hexadecimal to decimal. But I was not able to get the desired result. Can someone please pin point the error in this code?
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int hexaToDecimal(string n){
int ans = 0;
int power =1;
int s = n.size();
for(int i=s-1; i>=0; i--){
if(n[i] >= '0' && n[i] <= '9'){
ans = ans + power*(n[i]);
}
else if(n[i] >= 'A' && n[i] <= 'F'){
ans = ans + power*(n[i]-'A' + 10);
}
power = power * 16;
}
return ans;
}
int main(){
string n;
cin>>n;
cout<<hexaToDecimal(n)<<endl;
return 0;
}