Convert Roman number to arabic

JLChafardet 0 Tallied Votes 299 Views Share

This code was part of my first quarter 2nd semester project for a subject called programming 2.

its a simple roman to arabic convertion function.

#include <iostream>
#include <cstring>

using namespace std;

// prototype for function
int romano_dec();

int main(int argc, char *argv[])
{
romano_dec();
system("PAUSE");
return EXIT_SUCCESS;
}

int romano_dec()
{
     char roman_num[10];
	 cout << "Por favor ingrese un numero romano: ";
	 cin >> roman_num;
	 int len = strlen(roman_num);
	 int number = 0;
	 int counter = 0;
	 int b4sum = 0;
	 int sum = 0;

	 for (counter = len; counter >= 0; counter--)
		 {
			 if (roman_num[counter] == 'M' || roman_num[counter] == 'm')
				 number = 1000;
			 else if (roman_num[counter] == 'D' || roman_num[counter] == 'd')
				 number = 500;
			 else if (roman_num[counter] == 'C' || roman_num[counter] == 'c')
				 number = 100;
			 else if (roman_num[counter] == 'L' || roman_num[counter] == 'l')
				 number = 50;
			 else if (roman_num[counter] == 'X' || roman_num[counter] == 'x')
				 number = 10;
			 else if (roman_num[counter] == 'V' || roman_num[counter] == 'v')
				 number = 5;
			 else if (roman_num[counter] == 'I' || roman_num[counter] == 'i')
				 number = 1;
			 else
				 number = 0;

			 if (b4sum > number)
				 sum = b4sum - number;
			 else
				 sum = sum + number;
			 b4sum = number;
		}

	cout << "El resultado es: " ;
    cout << sum << endl;
}
kelechi96 0 Light Poster

In spanish but other than that its great

Gewalop 1 Newbie Poster

You actually have one problem, your function (romano_dec) is an INT function, and it must return some kinda a value, so if you only make it void, or return anything.

btw, you're code is nice and works perfect

JLChafardet 0 Newbie Poster

thank you for your input guys :)

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.