# convert Roman Numerals - problem

Page 1 of 1

## 4 Replies - 2319 Views - Last Post: 02 February 2011 - 01:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=213465&amp;s=a30cd42815644cb47a7d50ace9d3e2ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 XodoX

Reputation: 0
• Posts: 23
• Joined: 04-November 10

# convert Roman Numerals - problem

Posted 01 February 2011 - 09:08 PM

C++. I thought it worked, but it dosen't. ugh. It obviously only convert single letters, but not XL, MC etc. How do I do this? When I input XL, it will only convert X.

```#include <iostream>
using namespace std;

class romanType{
public:
romanType(char&);
int convert();
void print();
void get();

private:
int M, D, C, L, X, V, I;
char romanNumeral;
};
romanType::romanType(char &ch){
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
cout << ch << endl;
romanNumeral=ch;
}
int romanType::convert(){
switch (romanNumeral) {

case 'M': return 1000;

case 'D': return 500;

case 'C': return 100;

case 'L': return 50;

case 'X': return 10;

case 'V': return 5;

case 'I': return 1;

}
return 0;
}
void romanType::print(){
cout << romanNumeral << endl;
}
void romanType::get(){
}
int main(){
char romanNumeral;
cout << "Please enter a number to convert: ";
cin >> romanNumeral;
romanType roman=romanType(romanNumeral);
cout << roman.convert() << endl;;

system("pause");
return 0;
}

```

This post has been edited by XodoX: 01 February 2011 - 09:10 PM

Is This A Good Question/Topic? 0

## Replies To: convert Roman Numerals - problem

### #2 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

## Re: convert Roman Numerals - problem

Posted 01 February 2011 - 09:18 PM

You are only getting one character. May I suggest using a 4 or 8 character buffer?

However, then you have to take into account subtractions when you have a lower character before a higher character. Going from a one character roman numeral converter to a multi character is actually quite a step.

I would suggest getting the string from the user. Convert every character over to its decimal equivalent. Store those into another array. Maybe a structure or another class representing a string roman numeral could be used.

So now you have the decimal equivalent of every character. Look at the old string and see if any subtractions need to be made (Lower characters before higher i.e IV) Do the subtract if necessary and then store the value into some value in your class where it can be retrieved for later use.

Here is an example of what I mean:
IXX -> {1, 10, 10)
I < X -> subtraction needs to be made
Add up high and low parts and subtract 20 - 1
Store value -> 19
Retrieve value -> romanNumeral.getDecimal()

### #3 XodoX

Reputation: 0
• Posts: 23
• Joined: 04-November 10

## Re: convert Roman Numerals - problem

Posted 01 February 2011 - 09:23 PM

Thanks. Ughh, that sounds difficult.

### #4 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

## Re: convert Roman Numerals - problem

Posted 01 February 2011 - 09:24 PM

Well.. not really. All you need is two arrays and another if conditional.

This post has been edited by eker676: 01 February 2011 - 09:25 PM

### #5 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: convert Roman Numerals - problem

Posted 02 February 2011 - 01:42 AM

You might like to check the DIC snippets for some alt ideas ...