Roman to integer

Photo by Andrew Neel on Unsplash

Roman to integer

Hi peeps:) Today I solved a very easy problem which first may seem tricky but is very simple.

To convert a string from roman numerals to integers we should first know what the letters "I", "V" etc actually mean. For your reference I have listed the values of each character :

"I" = 1
"V" = 5
"X" = 10
"L" = 50
"C" = 100
"D" = 500
"M" = 1000

So, the code is very simple. We just compare the values of string characters to these roman characters and in cases like "IV", "CM" we subtract the value of I from V(5-1=4) and C from M(1000-100=900).

Let's go straight to our code :

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int sum=0;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='I'){
            if(s[i+1]=='V'){
                sum+=4;
                i++;
            }
            else if(s[i+1]=='X'){
                sum+=9;
                i++;
            }
            else if(s[i+1]=='L'){
                sum+=49;
                i++;
            }
            else if(s[i+1]=='D'){
                sum+=499;
                i++;
            }

            else if(s[i+1]=='C'){
                sum+=99;
                i++;
            }
            else if(s[i+1]=='M'){
                sum+=999;
                i++;
            }
            else{
                sum+=1;
            }

        }
        else if(s[i]=='V'){
            if(s[i+1]=='L'){
                sum+=45;
                i++;
            }

            else if(s[i+1]=='C'){
                sum+=95;
                i++;
            }
            else if(s[i+1]=='D'){
                sum+=495;
                i++;
            }
            else if(s[i+1]=='M'){
                sum+=995;
                i++;
            }
            else{
                sum+=5;
            }
        }
        else if(s[i]=='X'){
            if(s[i+1]=='L'){
                sum+=40;
                i++;
            }
            else if(s[i+1]=='C'){
                sum+=90;
                i++;
            }
            else if(s[i+1]=='D'){
                sum+=490;
                i++;
            }
            else if(s[i+1]=='M'){
                sum+=990;
                i++;
            }
            else{
                sum+=10;
            }
        }
        else if(s[i]=='L'){
            if(s[i+1]=='C'){
                sum+=50;
                i++;
            }
            else if(s[i+1]=='D'){
                sum+=450;
                i++;
            }
            else if(s[i+1]=='M'){
                sum+=950;
                i++;
            }
            else{
                sum+=50;
            }
        }
        else if(s[i]=='C'){
            if(s[i+1]=='D'){
                sum+=400;
                i++;
            }
            else if(s[i+1]=='M'){
                sum+=900;
                i++;
            }
            else{
                sum+=100;
            }
        }
        else if(s[i]=='D'){
            if(s[i+1]=='M'){
                sum+=500;
                i++;
            }

            else{
                sum+=500;
            }
        }
        else if(s[i]=='M'){
            sum+=1000;
        }
    }

    cout<<sum<<endl;
}

I hope you got the logic. If not, feel free to write in the comment box :) Also, I would love to learn more if you have a better approach to this problem.