# Roman number converter

• 04-21-2002
BalanusBob
Roman number converter
Does anyone know any good code for converting Arabic numbers to Roman numbers? I need c++ code that can be used on Windows C++ Visual Studio. Appreciate any help that I can get.
• 04-21-2002
Dual-Catfish
Go nuts with modulus. It will give you the remainders of each division, 1000, 100, 50, 5, whatever, and you can output the correct letter.. or set of letters.
• 04-21-2002
Uraldor
smells like homework to me.

how about you try and do it yourself and post with specific questions.
• 04-22-2002
bob2509
Use switch statement as follows:

Code:

while (loopCount < firstRoman.length())
{
switch(firstRoman[loopCount++])
{
case 'I':
first += 1;
break;

case 'V':
first += 5;
break;

case 'X':
first += 10;
break;

case 'L':
first += 50;
break;

case 'C':
first += 100;
break;

case 'D':
first += 500;
break;

case 'M':
first += 1000;
break;

default:
cout << endl << " You entered an invalid numeral. Goodbye." << endl << endl;

return;

• 04-22-2002
Unregistered
the capital letters MDCLXVI are constants and these converts the integer to the Roman numeral (adding style) ie no IV for 4. and it is for straight roman numerals no extended.
Code:

string Convert(int digit)
{
string temp="\0";

while (digit != 0)
{
if (digit > 999)
{
digit = digit - M;
temp = temp + 'M';
}
else
if (digit > 499)
{
digit = digit - D;
temp = temp + 'D';
}
else
if (digit >99)
{
digit = digit - C;
temp = temp + 'C';
}
else
if (digit > 49)
{
digit = digit - L;
temp = temp + 'L';
}
else
if (digit > 9)
{
digit = digit - X;
temp = temp + 'X';
}
else
if (digit > 4)
{
digit = digit - V;
temp = temp + 'V';
}
else
{
digit = digit - I;
temp = temp + 'I';
}
}//end while
return (temp);
}//end Convert ()

• 04-22-2002
Gr3g
convert
I know its ugly but....

Code:

#include <iostream.h>
/*
i 1
v 5
x 10
l 50
c 100
d 500
m 1000
*/
int main()
{
int equiv2[7];
char equiv1[7];

equiv2[0]=1;
equiv2[1]=5;
equiv2[2]=10;
equiv2[3]=50;
equiv2[4]=100;
equiv2[5]=500;
equiv2[6]=1000;

equiv1[0]='I';
equiv1[1]='V';
equiv1[2]='X';
equiv1[3]='L';
equiv1[4]='C';
equiv1[5]='D';
equiv1[6]='M';

char enter[20];
int tmp[20],
out=0,
z;

for(int i=0; i<20; i++)
enter[i]=' ';
for(i=0; i<20; i++)
tmp[i]=0;

cout<<"enter the roman numeral:";
cin>>enter;

i=0;
while(i<20&&enter[i]!=' ')
{
for(z=0; z<7; z++)
{
if(equiv1[z]==enter[i])
{
tmp[i]=equiv2[z];
break;
}
}
i++;
}
i=0;
z=0;
while(i<20&&enter[z]!=' ')
{
if(tmp[i+1]>tmp[i])
out+=tmp[i+1]-tmp[i];
else
out+=tmp[i+1]+tmp[i];

i+=2;
z++;
}

cout<<endl<<out<<endl;

return 0; //notice the skillful use of int main, and how i return values ;)
}

• 04-22-2002
lostminds
this sounds like some one else in my class hmm... don't use arrays for they have not been taught in class yet *cough* You need at least 3 functions:

1 - Convert from integer to Roman Numeral
2 - Convert from Roman Numeral to integer
3 - Check Roman Numeral for errors (arrangement and false letters)

Of course they would be value returning like the string function I posted above.
• 04-22-2002
BalanusBob
I am evidently not in your class, because we had arrays weeks ago and i can use them in this function. The array function code that you posted is for converting from Roman to Arabic, not from Arabic to Roman. I presume I can just change some of the variables around and it will work. Thanks for the help.
• 04-23-2002
lostminds
I meant the string returning function up above the arrays that converts from an integer to a Roman Numeral. Not in my class , oh well. cheers.