4 Replies - 1938 Views - Last Post: 02 February 2011 - 01:42 AM Rate Topic: -----

#1 XodoX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • 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()
Was This Post Helpful? 0
  • +
  • -

#3 XodoX  Icon User is offline

  • New D.I.C Head

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

Re: convert Roman Numerals - problem

Posted 01 February 2011 - 09:23 PM

Thanks. Ughh, that sounds difficult.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • 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 ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1