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.

Recommended Answers

All 4 Replies

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".

oh it works already. thanks

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.