0

Kindly pl correct my if-else statement if you get the position where i made the mistake.Pl ignore space-time complexity for this problem.Ignore conditions i have checked they are correct.I have correct conditions applied,i am thinking of using switch statements instead of if-else.Tell me the mistakes so that i can correct them in my if statements.

class Solution {
public:
    int romanToInt(string s) {
        int sum = 0;
        int g = s.length();
        for(int start=0;start<g;start++){
            int k = (start-1);
            if(s[start]== 'V' || s[start]== 'X' && k>=0){
                if(s[k]=='I'){
                    if(s[start]=='V'){
                        sum=sum+4;
                    }
                    if(s[start]=='X'){
                        sum=sum+9;
                    }
                }
                else{
                    if(s[start]== 'V'){
                        sum=sum+5;
                    }
                    if(s[start]=='X'){
                        sum=sum+10;
                    }
                }
            }
            if(s[start]=='L' || s[start]=='C'&& k>=0 ){
                if(s[k]=='X'){
                    if(s[start]=='L'){
                        sum = sum+40;
                    }
                    if(s[start]=='C'){
                        sum = sum+90;
                    }
                }
                else{
                    if(s[start]=='L'){
                        sum = sum+50;
                    }
                    if(s[start]=='C'){
                        sum = sum+100;
                    }
                }
            }
            if(s[start]=='D' || s[start]=='M' && k>=0){
                if(s[k]=='C'){
                    if(s[start]=='D'){
                        sum = sum+400;
                    }
                    if(s[start]=='M'){
                        sum = sum+900;
                    }
                }
                else{
                    if(s[start]=='D'){
                        sum = sum+500;
                    }
                    if(s[start]=='M'){
                        sum = sum+1000;
                    }
                }
            }
            if(s[start]=='I' && s[start+1] !='V' || s[start+1] !='X'){
                sum = sum+1;
            }
            if(s[start]=='X' && s[start+1] !='L' || s[start+1] !='C'){
                sum = sum+10;
            }
            if(s[start]=='C' && s[start+1] !='D' || s[start+1] !='M'){
                sum = sum+100;
            }
        }
        return sum;
    }
};
VLL
  • 9,634
  • 1
  • 29
  • 54
  • `s[start+1]` is out of bounds when you are checking the last character. You might consider taking some time to make this a [mcve] and improve the formatting so it is more readable. Using a debugger to step through the code to see what is happening would help you find ant issues on your own. – Retired Ninja Oct 23 '20 at 06:17
  • if think pl check my else statements for C ie Century and other else statements –  Oct 23 '20 at 06:17
  • i can't find the realtime. How can i convert it to minimal reproducible example.Sorry for the troubles. –  Oct 23 '20 at 06:19
  • Did you read the link which describes how to make a MRE? – Yunnosch Oct 23 '20 at 06:21
  • i don't know how to use a debugger can someone share the stackoverflow article which explains it ?? @RetiredNinja –  Oct 23 '20 at 06:21
  • The problem of multiple expected output is not within the shown code. An MRE is very important. – Yunnosch Oct 23 '20 at 06:22
  • https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://stackoverflow.com/questions/8041614/how-to-debug-in-codeblocks/58050981#58050981 – Yunnosch Oct 23 '20 at 06:22
  • "i can't find the realtime" is cryptic to me. "I have correct conditions applied" and "Tell me the mistakes so that i can correct them in my if statements." seem to contradict each other. – Yunnosch Oct 23 '20 at 06:24
  • You need `()` around your `||` clauses, otherwise `if(s[start]=='X' && s[start+1] !='L' || s[start+1] !='C'){` basically means `if (s[start+1] != 'C')` – Retired Ninja Oct 23 '20 at 06:26
  • @RetiredNinja if((s[start]=='C' && s[start+1] !='D') || s[start+1] !='M') is this correct ?? –  Oct 23 '20 at 06:32
  • Why is my question closed???? –  Oct 23 '20 at 06:35
  • so we have to use debugger and read the MRE instructions to get an answer on stackoverflow ?? @mods –  Oct 23 '20 at 06:51

0 Answers0