Why this displays 5 for "IV" input?

class Program
{
    static void Main(string[] args)
    {
               /*
                I              1
                V             5
                X             10
                L             50
                C             100
                D             500
                M             1000
               */

            string str;
            int cnt = 0, i = 0;
            str = Console.ReadLine();
            while (i<str.Length)
            {
                if (str[i] == 'M')
                    cnt = cnt + 1000;
                if (str[i] == 'D')
                    cnt = cnt + 500;
                if (str[i] == 'C')
                    cnt = cnt + 100;
                if (str[i] == 'L')
                    cnt = cnt + 50;
                if (str[i] == 'X')
                    cnt = cnt + 10;

                if (str[i] == 'I' && str[i + 1] == 'V')
                    cnt = cnt + 4;
                else
                    if (str[i] == 'I' && str[i + 1] != 'V')
                        cnt = cnt + 1;

                if (str[i] == 'V' && str[i - 1] == 'I')
                    cnt = cnt + 0;
                else
                    if (str[i] == 'I' && str[i + 1] != 'I')
                        cnt = cnt + 1;
                i++;

            }

            Console.WriteLine(cnt);

    }
}

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

I’m trying to transform roman numbers to integers. This is a bug.

What is a bug? How do we see the bug? What have you tried so far to fix the bug?

I should get “4” for the “IV” input. On paper with pen but no luck so far. It’s my try for the https://leetcode.com/problems/roman-to-integer/ problem.