-2

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?

  • 4
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), and [what is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – PaulMcKenzie Aug 26 '21 at 05:58

2 Answers2

5

i++ and ++i and i + 1 does three different things.

And because how i++ works, str[i]=='I' and str[i++]=='V' will actually the equivalent to str[i]=='I' and str[i]=='V', which is always false.

And i++ (or ++i) is totally wrong here, you need i + 1 and then an increment inside the body of the if. As is the call to substr which isn't needed, and which returns the sub-string anyway, so it doesn't actually do anything useful.

Like for example:

if(str[i] == 'I' and str[i + 1] == 'V') {
    num=num + 4; 
    ++i;
}

As for why i++ (and ++i) is wrong inside the condition itself, think about if the condition str[i]=='I' is true but str[++i]=='V' is false... Then you will increase i before the next condition is checked:

// Begins with i == 0
if(str[i]=='I' and str[++i]=='V') { ... }
// If the above condition is false, this will be checked
// BUT! Here i might be equal to 1 because of the above increment
else if(str[i]=='I' and str[++i]=='X') { ... }
...
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

i is increased each if
this is how it should be

    for(int i=0; i<str.size(); ){
    .......
    else if(str[i]=='X' and str[i + 1]=='C'){
        num += 90;
        ...
        i += 2;
    }
    ..........
    else i++;
    }
Errorist
  • 224
  • 2
  • 6