Start New Discussion within our Software Development Community

A roman to decimal converter, no validity checking, so inputting an invalid roman number will certainly just yield a wrong result.

/* A Roman to Decimal converter */

#include <iostream>

int romToDec(char *p);
int valueOf(char c);

int main()
{
    char rom[50];

    std::cout << "Roman: ";
    std::cin.getline(rom, 50);

    std::cout << "Decimal: " << romToDec(rom) << std::endl;

    std::cin.ignore(1000, '\n');

    return 0;
}

int romToDec(char *p)
{
    int result = 0;

    for(; *p; p++)
    {
        int current = valueOf(*p);
        int next = valueOf(*(p+1));

        if(current >= next) result += current;
        else result -= current;
    }

    return result;
}

int valueOf(char c)
{
    switch(c)
    {
        case 'M': return 1000;
        case 'D': return 500;
        case 'C': return 100;
        case 'L': return 50;
        case 'X': return 10;
        case 'V': return 5;
        case 'I': return 1;

        default: return 0;
    }
}
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.