Hi I am making a roman numeral to decimal converter, I have the decimal to roman numeral converter but I am having trouble doing it from roman to decimal.
How do you pull a string apart letter by letter; eg VXI, into 3 variables a= V, b= X, c= I.
do you use this somehow?
cin.ignore(100, ' ');
Also my code so far is this, I am all for efficiency so if you think that something could be written in a easier format or with less lines I'd be really interested :D
#include <iostream>
using namespace std;
int numeral_converter(int a);
void roman(int a, int b, int c, int d);
int main()
{
int n=0;
cout << "Enter a number: ";
cin >> n;
numeral_converter(n);
return 0;
}
int numeral_converter(int a)
{
int one, ten, hund, tho;
if (a >= 100)
ten= (a/10)%10;
else ten= a/10;
if (a >= 1000)
hund= (a/100)%10;
else
hund= a/100;
one= a%10;
tho= a/1000;
cout << "Thousands= "<< tho << " Hundreds= " << hund;
cout << " Tens= " << ten << " Ones= " << one << endl;
roman(one, ten, hund, tho);
return a;
}
void roman(int a, int b, int c, int d)
{
for (int l= 0; l < d; l++)
{
cout << "M";
}
for (int k= 0; k < c; k++)
{
cout << "C";
}
if (b == 4)
cout << "XL";
else if ( b>= 5)
{
b = b-5;
cout << "L";
for (int j= 0; j < b; j++)
{
cout << "X";
}
}
else
for (int j= 0; j < b; j++)
{
cout << "X";
}
if (a == 4)
cout << "IV";
else if ( a>= 5)
{
a= a-5;
cout << "V";
for (int i= 0; i < a; i++)
{
cout << "I";
}
}
else
for (int i= 0; i < a; i++)
{
cout << "I";
}
}