My code converts roman numerals to the number values but i dont know how to make it do deciamls too, like for example: XX-X. I need it to recognize the "-" and start the loop again. How do i do that?

Here is the code:

#include <iostream>
#include <string>


using namespace std;



int ret_letter(char ch)
{	
	
	switch (ch)	
{	
case 'I':		
	return 1;	
case 'V':		
	return 5;	
case 'X':		
	return 10;	
case 'L':		
	return 50;	
case 'C':		
	return 100;	
case 'D':		
	return 500;	
case 'M':		
	return 1000;
default:		
	return -1;	
	}

}
int main()
{	
	char str[100];	
	
	cout << "Pleas enter a Roman Numeral " ;
	cin >> str;	

	

	int len = strlen(str),x,sum = 0;	
	x = len-1;	
	
	int last_letter = ret_letter(str[x--]);	
	
	sum = last_letter;	
	
	while (x >= 0)	
	{		
		if (last_letter <= ret_letter(str[x]))
		{
			sum = sum + ret_letter(str[x]);
		}
		else
		{
			sum = sum - ret_letter(str[x]);
		}
		last_letter = ret_letter(str[x]);		
		x--;	
	}	

	cout << "The numerical value is: " <<  sum << endl;	
	
	return 0;
}

Recommended Answers

All 3 Replies

Roman numerals don't have decimals. It's therefore undefined so you can pretty much do whatever you want.

if you are seperating the whole part and decimal part then i would make your routine for getting the number into its own function and then in main parse through the string untill you hit the '-' then save that substring as your whole part and the rest will be your decimal part. the tricky part is taking to ints and making a decimal. for that i would use 3 function calls. first i would call itoa on both the whole part and decimal part. then combine both of those strings with a decimal point in bettwen them. then i woulld call strtodnvert the decimal string into a float.

itoa() is not a standard function and may not exist in your compiler.

And strtodnvert() doesn't seem to exist in Google at all.

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.