Please give me the logic or a simple program to convert a Roman letter to Numeral . Please.......

Recommended Answers

All 4 Replies

Question: Did you ever try the forum search function?
Answer: NO, NO, NO and NO
Proof: http://www.daniweb.com/code/snippet912.html

Ehh, this link refers to a very strange, ineffective and incorrect roman to arabic numerals converter. Moreover: it's impossible to compile this snippet w/o errors with a good compiler...
Proof: try to convert XCIX == 99 (and many many others)...
Better look at http://en.wikipedia.org/wiki/Roman_Numeral and do it yourself ;)...

Ok this is really simplistic code, I have just started learning programming but its a good start to convert roman to numbers. Plus its helping me understand C++ more, & as you can see I have just learnt switch structures :P But you can only convert a roman numeral that is one letter.

On a side note, does anyone know how I could change this code to read roman numerals of more than one character and return the number? Also is there a better way of doing this instead of switch structures?

#include <iostream>
#include <string>

using namespace std;


int main()
{
	char n = 'a';
	int nn;
	cout << "Please enter a Roman numeral: ";
	cin >> n;
	cout << "Roman numeral= " << n << endl;

	switch (n)
	{

	case 'm':
	case 'M':
		nn = 1000;
		cout << "Number= " << nn;
		break;
	case 'l':
	case 'L':
		nn = 100;
		cout << "Number= " << nn;
		break;
	case 'x':
	case 'X':
		nn = 10;
		cout << "Number= " << nn;
		break;
	case 'v':
	case 'V':
		nn = 5;
		cout << "Number= " << nn;
		break;
	case 'i':
	case 'I':
		nn = 1;
		cout << "Number= " << nn;
		break;
	default:
		cout << "This is not a Roman Numeral!";
	}

	return  0;
}

I would use a map from STL. Actually, map is a better data structure to be used in this case.
To the OP:
It is still not clear if you want to convert a Roman letter (like 'V' 'I' 'L' 'M') or a whole roman number( like 'IV' 'VII' ). The former is trivial but latter may require some skills.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.