My code converts roman numerals to the number values but i dont know how to make it do deciamls too, like for example: XX-X. I need it to recognize the "-" and start the loop again. How do i do that?
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int ret_letter(char ch)
{
switch (ch)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return -1;
}
}
int main()
{
char str[100];
cout << "Pleas enter a Roman Numeral " ;
cin >> str;
int len = strlen(str),x,sum = 0;
x = len-1;
int last_letter = ret_letter(str[x--]);
sum = last_letter;
while (x >= 0)
{
if (last_letter <= ret_letter(str[x]))
{
sum = sum + ret_letter(str[x]);
}
else
{
sum = sum - ret_letter(str[x]);
}
last_letter = ret_letter(str[x]);
x--;
}
cout << "The numerical value is: " << sum << endl;
return 0;
}