// Convert Roman Numerals to decimal
// By DreamVB 20:43 22/10/2016

#include <iostream>

using namespace std;
using std::cout;
using std::endl;

int main(int argc, char *argv[]){

	int X = 0;
	int Y = 0;
	int result = 0;
	int value = 0;
	bool IsVaild = true;
	char ch = '\0';
	char prev = '\0';
	char sRoman[31];
	char *VaildSet = "IVXLCDM";

	cout << "Enter in roman numeral string : ";
	cin >> sRoman;

	//Get length of Roman numeral
	X = strlen(sRoman) - 1;

	while (X >= 0){
		//Get letter convert to uppercase
		ch = toupper(sRoman[X]);

		//Check for vaild Roman numeral chars
		if (strchr(VaildSet, ch) == NULL){
			IsVaild = false;
			break;
		}

		//Convert roman number letter to decimal
		if (ch == 'I') value = 1;
		if (ch == 'V') value = 5;
		if (ch == 'X') value = 10;
		if (ch == 'L') value = 50;
		if (ch == 'C') value = 100;
		if (ch == 'D') value = 500;
		if (ch == 'M') value = 1000;

		if ((value < result) && (ch != prev)){
			//Minus value from sum
			result -= value;
		}
		else{
			//Plus value to sum
			result += value;
		}
		//Get old char
		prev = ch;
		//DEC counter
		X--;
	}

	if (!IsVaild){
		cout << "Error the string was not a vaild roman numeral string." << endl;
	}
	else{
		//Output result
		cout << "Decimal value of " << sRoman << " is " << result << endl;
	}
	
	system("pause");
	return 0;
}