im having a problem with my converter.

this is my code :

void romanNumeral::convert(){
     for(int i=0; i < roman.length(); i++){
        
              letter = roman.at(i);
              
              switch(letter){
              case 'M': 
              case 'm': decimal += 1000; 
              break;
              
              case 'D': 
              case 'd': decimal += 500; 
              break;
              
              case 'C': 
              case 'c': decimal += 100; 
              break;
              
              case 'L': 
              case 'l': decimal += 50; 
              break;
              
              case 'X': 
              case 'x': decimal += 10; 
              break;
              
              case 'V': 
              case 'v': decimal += 5; 
              break;
              
              
              case 'I': 
              case 'i': decimal += 1;
              
              break;
              }         
     }
     cout << decimal;
}

is there any way to detect that "iv" is 4 and not 6. and some of the roman numerals like "ix" should be 9 and not 11.

Try it,

.....      
      case 'I': 
      case 'i': 
               if(roman.at(i+1)=='V' || roman.at(i+1)=='X')
                  decimal-=1;
              else
                  decimal += 1;
....

its still the same. it gives me 6 if i enter "iv".

Try it,

.....      
      case 'I': 
      case 'i': 
               if(roman.at(i+1)=='V' || roman.at(i+1)=='X')
                  decimal-=1;
              else
                  decimal += 1;
....

Problem with this code is that when 'i' reaches 'roman.length()', roman.at(i+1) will be out of bounds. -> Crash

@OP:
have a look at this

Edited 7 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.