Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,929 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,837 people online right now. Registration is fast and FREE... Join Now!




convert roman number to decimal number.(different with previous one)

 
Reply to this topicStart new topic

convert roman number to decimal number.(different with previous one), SOS.emergency

andrewtan
1 Apr, 2008 - 02:20 AM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 2

kindly help me .i need this one C code
start by #include<stdio.h>

User is required to enter a Roman number to be converted Decimal number. The symbols used in Roman numeral system and their equivalents are given below:

Roman Decimal
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

You have to ask the user to re-enter a Roman number if the user enter any other characters not given in the table above.

Steps to convert:
1. Set the value of the decimal number to zero.
2. Check the string containing the Roman characters from left to right:
- if the next character is a null character (if the current character is the last character), add the value of the current character to the decimal value.
- if the value of the current character is greater than or equal to the value of the next character, add the value of the current character to the decimal value.
- if the value of the current character is less than the next character, subtract the value of the current character from the decimal value.

Example 1:
Please enter Roman number: XL
The equivalent Decimal number is: 40

Example 2:
Please enter Roman number: XCVIII
The equivalent Decimal number is: 98

Example 3:
Please enter Roman number: MDCCXXIX
The equivalent Decimal number is: 1729

Example 4:
Please enter Roman number: MNCI
Invalid input!
Please re-enter Roman number: MCQ
Invalid input!
Please re-enter Roman number: DXC
The equivalent Decimal number is: 590

User is offlineProfile CardPM
+Quote Post

syazhani
RE: Convert Roman Number To Decimal Number.(different With Previous One)
1 Apr, 2008 - 02:27 AM
Post #2

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 33


My Contributions
This won't work, you've shown no effort of doing it except telling "start with #include <stdio.h>".

I'm new here and from my observation people over here would like to see your effort in doing so they can point you to the right direction and/or help you with codes.

...a tip though, try googling, this problem is very common so I am sure you will find the solution...
User is offlineProfile CardPM
+Quote Post

andrewtan
RE: Convert Roman Number To Decimal Number.(different With Previous One)
1 Apr, 2008 - 03:59 AM
Post #3

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 2

QUOTE(syazhani @ 1 Apr, 2008 - 03:27 AM) *

This won't work, you've shown no effort of doing it except telling "start with #include <stdio.h>".

I'm new here and from my observation people over here would like to see your effort in doing so they can point you to the right direction and/or help you with codes.

...a tip though, try googling, this problem is very common so I am sure you will find the solution...


thanks for ur suggestion. acutally i done it but lack something.i need to do the error checking so that the program will show the example output :
Example 4:
Please enter Roman number: MNCI
Invalid input!
Please re-enter Roman number: MCQ
Invalid input!
Please re-enter Roman number: DXC
The equivalent Decimal number is: 590

help!!!

can anyone help me to modify my code ?

the code of mine shown below: i think the red color part got problem,kindly help me to solve it .thanks

#include <stdio.h>
#include <string.h>
#include<ctype.h>
int letterValue(char letter);
int convert(char*roman);
int main()
{
int arabicValue,j=0;
char romanString[50];
printf("Please enter a Roman number:");


do
{ gets(romanString);
printf("Invalid input\n");
printf("Please re-enter Roman number:");
}while(romanString[j]!="I,V,X,L,C,D,M");
arabicValue=convert(romanString);


printf("The equivalent Decimal number is %d\n",arabicValue);

return 0;
}
int convert(char *roman)
{
int arabic = 0, tmp;
char *str_p = roman;

while(*str_p)
{
tmp = letterValue(*str_p);
if (letterValue(*++str_p) > tmp)
arabic -= tmp;
else
arabic += tmp;
}
return arabic;
}

int letterValue(char letter)
{
if (toupper(letter) == 'I') return 1;
if (toupper(letter) == 'V') return 5;
if (toupper(letter) == 'X') return 10;
if (toupper(letter) == 'L') return 50;
if (toupper(letter) == 'C') return 100;
if (toupper(letter) == 'D') return 500;
if (toupper(letter) == 'M') return 1000;
return 0;
}
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month