Roman Numeral to Decimal conversion

I'm working on a converting Roman numbers to Decimal problem. My lackluster code will output a single roman character as decimal form, but that gets me very little. My idea was that I could store a multi-charactered roman number, and possibly iterate the characters through the switch statement, then use the modulo operator in another switch or if/else succession to print the number. I don't feel like I'm gaining any traction with the problem and could use some advice to get me going in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char romNum;
    int decimal;
    
    cout << "Input a Roman Number and I will tell you its integer equivalent "     << endl;
    cin >> romNum;
    
    switch(romNum)
    {
            case 'M': decimal = 1000; break;
            case 'D': decimal = 500; break;
            case 'C': decimal = 100; break;
            case 'L': decimal = 50; break;
            case 'X': decimal = 10; break;
            case 'V': decimal = 5; break;
            case 'I': decimal = 1; break;
            default : cout << "Not a valid roman numeral"; break;
        
            return decimal;
    }
    cout << " the roman numeral is " << decimal << endl;
}
str = DLXII
sum = 500+50+10+1+1 = 562

str = IV
sum(1) = 1
sum(2) = 1<5 so 5-1 = 4
you can try getting the char one by one(from the string):

////NOTE! romNum is changed to string!! because they are more manageable :)
////decimal is initiated as int decimal = 0
for(unsigned int i = 0;i<romNum.size;i++)
switch(romNum.c_str()[i])
case 'M': decimal += 1000; break; ///so on....


and as a side note....there are IV, IX, so on, and XV, so you have to deal with algorithms for those too. But I guess it will become more easy by now. Good luck for that. :)
Topic archived. No new replies allowed.