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;
	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]);
			sum = sum - ret_letter(str[x]);
		last_letter = ret_letter(str[x]);		

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

Edited 6 Years Ago by WaltP: Added CODE Tags

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.

This article has been dead for over six months. Start a new discussion instead.