Computing.Net > Forums > Programming > Roman Numeral Conversion (C)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Roman Numeral Conversion (C)

Reply to Message Icon

Name: aoami
Date: December 15, 2008 at 19:57:16 Pacific
OS: XP Pro 64
CPU/Ram: AthlonX2 4gb RAM
Product: Amd / ?
Comment:

[code]
void UserConvert(void)

{
int num1;

printf("You have chosen to input the Arabic Number you wish to convert yourself. Please enter an Arabic Number: \n");
scanf("%i", &num1);

while (num1 >= 1000)
{
printf("M");
num1 -= 1000;
}
if (num1 >= 900)
{
printf("CM");
num1 -= 900;
}
if (num1 >= 500)
{
printf("D");
num1 -= 500;
}
if (num1 >= 400)
{
printf("CD");
num1 -= 400;
}
while (num1 >= 100)
{
printf("C");
num1 -= 100;
}
if (num1 >= 90)
{
printf("XC");
num1 -= 90;
}
if (num1 >= 50)
{
printf("L");
num1 -= 50;
}
if (num1 >= 40)
{
printf("XL");
num1 -= 40;
}
while (num1 >= 10)
{
printf("X");
num1 -= 10;
}
if (num1 >= 9)
{
printf("IX");
num1 -= 9;
}
if (num1 >= 5)
{
printf("V");
num1 -= 5;
}
if (num1 >= 4)
{
printf("IV");
num1 -= 4;
}
while (num1 >= 1)
{
printf("I");
num1 -= 1;
}
}

---------

void main()

{
int userchoice;

do
{
printf("Welcome to the numeral conversion program. Please press 1 to enter the Arabic Number you wish to convert, or 2 to convert a list of numbers: \n");
scanf("%d", &userchoice);
}
while ((userchoice != 1) && (userchoice != 2));

if (userchoice == 1)
UserConvert(); //function called here.
else if (userchoice == 2)
BatchConvert();
else;
}[/code]

Hi guys. I have to write a program in C to convert Arabic Numbers to Roman Numerals. This program compiles, but when i type in any number the result i get is MMDDCCLLXXVVI followed by a string of random characters. Does anyone have any idea why this could be?

ps. don't worry about the BatchConvert function, one step at a time atm! :P

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 15, 2008 at 21:48:09 Pacific
Reply:

Would you care if this was written in Batch?

@echo off
ECHO Arabic Number?
SET /p Arabic1=
SET arabic=%arabic1%

:1000
IF %Arabic% GEQ 1000 (
SET M=%M%M
SET /a Arabic=Arabic-1000
) ELSE (
GOTO 900
)
GOTO 1000

:900
IF %Arabic% GEQ 900 (
SET CM=CM
SET /a Arabic=Arabic-900
)


IF %Arabic% GEQ 500 (
SET D=D
SET /a Arabic=Arabic-500
)

IF %Arabic% GEQ 400 (
SET CD1=CD1
SET /a Arabic=Arabic-400
)

:100
IF %arabic% GEQ 100 (
SET C=%C%C
SET /a arabic=arabic-100
) ELSE (
GOTO 90
)
GOTO 100

:90
IF %arabic% GEQ 90 (
SET XC=XC
SET /a arabic=arabic-90
)

IF %arabic% GEQ 50 (
SET L=L
SET /a arabic=arabic-50
)

IF %arabic% GEQ 40 (
SET XL=XL
SET /a arabic=arabic-40
)

:10
IF %arabic% GEQ 10 (
SET x=%X%X
SET /a arabic=arabic-10
) ELSE (
GOTO 9
)
GOTO 10

:9
IF %arabic% GEQ 9 (
SET IX=IX
SET /a Arabic=arabic-9
)

IF %arabic% GEQ 5 (
SET V=V
SET /a Arabic=arabic-5
)

IF %arabic% GEQ 4 (
SET IV=IV
Set /a arabic=arabic-4
)

:1
IF %arabic% GEQ 1 (
SET I=%I%I
SET /a arabic=arabic-1
) ELSE (
GOTO END
)
GOTO 1
:end
SET r=%M%%CM%%D%%cd1%%C%%XC%%L%%XL%%X%%IX%%V%%IV%%I%
ECHO %arabic1% Is %r%

PAUSE

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: nails
Date: December 16, 2008 at 19:14:36 Pacific
Reply:

I ran your "C" program on my Solaris 9 machine and if worked with no problems. I think your problem is that your compiler probably doesn't like using printf without a field qualifier.

For example, instead of this:

printf("M");

try this:

printf("%s", "M");

I'd change all your printf's and see what happens.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Batch file to Stop/Start ... Unable to reinstall Visua...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Roman Numeral Conversion (C)

Roman Numerals in C language www.computing.net/answers/programming/roman-numerals-in-c-language/13872.html

RE-POST: Need help in C program... www.computing.net/answers/programming/repost-need-help-in-c-program/4285.html

(Visual Basic)Roman Numeral help www.computing.net/answers/programming/visual-basicroman-numeral-help/9988.html