This is the question that I am referring to: https://leetcode.com/problems/roman-to-integer/
And this is the code that I have used for the following:
int romanToDecimal(string &str) {
int num=0;
for(int i=0; i<str.size(); i++){
//three special cases given
if(str[i]=='I' and str[i++]=='V'){
num=num+4;
str.substr(i+2);
}
else if(str[i]=='I' and str[i++]=='X'){
num=num+9;
str.substr(i+2);
}
else if(str[i]=='X' and str[i++]=='L'){
num=num+40;
str.substr(i+2);
}
else if(str[i]=='X' and str[i++]=='C'){
num=num+90;
str.substr(i+2);
}
else if(str[i]=='C' and str[i++]=='D'){
num=num+400;
str.substr(i+2);
}
else if(str[i]=='C' and str[i++]=='M'){
num=num+900;
str.substr(i+2);
}
else if(str[i]=='I'){
num=num+1;
}
else if(str[i]=='V'){
num=num+5;
}
else if(str[i]=='X'){
num=num+10;
}
else if(str[i]=='L'){
num=num+50;
}
else if(str[i]=='C'){
num=num+100;
}
else if(str[i]=='D'){
num=num+500;
}
else if(str[i]=='M'){
num=num+1000;
}
}
return num;
}
It always ends up giving a wrong answer, not iterating and adding the numbers further. Why?