### Roman Numeral to Decimal Converter

I am trying to make a program that converts roman numerals to decimal form by adding and subtracting. EX: LX = 60. I would like to know if im doing this right and if what mistakes I may have made. If anyone can help it would be greatly appreciated.

This is my header file.
 ``123456789101112131415161718192021`` ``````#include using namespace std; class romanNO { public: void get(); void show(); romanNO(char&); int convert(); int length(); private: int M,D,C,L,X,V,I; char romanNum; };``````

This my main file.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````#include #include "roman.h" #include using namespace std; int main() { char romanNum; cout << "Welcome to the Roman Numeral Converter. Please enter the number you would like to convert: "; cin >> romanNum; int sum = 0; int i; int length; int previous = 1000; for(i = 0; i < length; i++) { switch(romanNO[i]) { case 'M': sum += 1000; if(previous < 1000) { sum -= 2 * previous; } previous = 1000; break; case 'D': sum += 500; if(previous < 500) { sum -= 2 * previous; } previous = 500; break; case 'C': sum += 100; if(previous < 100) { sum -= 2 * previous; } previous = 100; break; case 'L': sum += 50; if(previous < 50) { sum -= 2 * previous; } previous = 50; break; case 'X': sum += 10; if(previous < 10) { sum -= 2 * previous; } previous = 10; break; case 'V': sum += 5; if(previous < 5) { sum -= 2 * previous; } previous = 5; break; case 'I': sum += 1; if(previous < 1) { sum -= 2 * previous; } previous = 1; break; } return 0; } romanNO roman = romanNO(romanNum); cout << roman.convert() << endl; system("pause"); return 0; }``````

This is my .cpp file.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include "roman.h" using namespace std; romanNO::romanNO(char& ch) { M = 1000; D = 500; C = 100; L = 50; X = 10; V = 5; I = 1; cout << ch << endl; romanNum = ch; } int romanNO::convert() { /*if(romanNum == 'M') { return 1000; } else if(romanNum == 'D') { return 500; } else if(romanNum == 'C') { return 100; } else if(romanNum == 'L') { return 50; } else if(romanNum == 'X') { return 10; } else if(romanNum == 'V') { return 5; } else if(romanNum == 'I') { return 1; }*/ return 0; } void romanNO::show() { cout << romanNum << endl; } void romanNO::get() { } ``````
looks like you are doing cpp, this is using all your code you already had with a minor change.

So I will suggest this:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````#include #include using namespace std; class RomanNumber { public: string get() {return RomanNumber;} void show() { cout << "Roman Number is " << romanNumber << endl; } RomanNumber(const string &input); { romanNumber = input; } int convert(); { int length = romanNumber.length(); int previous = 0; bool error = false; int nIndex = 0; sum = 0; while( (error == false) && (nIndex < length) ) { switch(romanNumber[nIndex]) { case 'M': sum += 1000; if(previous < 1000) { sum -= 2 * previous; } previous = 1000; break; case 'D': sum += 500; if(previous < 500) { sum -= 2 * previous; } previous = 500; break; case 'C': sum += 100; if(previous < 100) { sum -= 2 * previous; } previous = 100; break; case 'L': sum += 50; if(previous < 50) { sum -= 2 * previous; } previous = 50; break; case 'X': sum += 10; if(previous < 10) { sum -= 2 * previous; } previous = 10; break; case 'V': sum += 5; if(previous < 5) { sum -= 2 * previous; } previous = 5; break; case 'I': sum += 1; if(previous < 1) { sum -= 2 * previous; } previous = 1; break; default: cout << romanNumber[nIndex] << " is not a Roman Numeral!" << endl; error = true; sum = 0; } // switch nIndex++; } // while return sum; } int length() {return romanNumber.length();} private: string romanNumber; int sum; }; int main() { string myInput = "LX"; RomanNumber myRomanNumber(myInput); int value = myRomanNumber.convert(); cout << "Roman Number " << myInput << " equals " << value <

Hope this points you in a direction.

If you can't use string of the Standard library, you could replace with char romanNumber[30] which is a string of characters with a length of 30.
As the program is right now, you'll still get a few errors. You need to remove the semi-colons on lines 17 and 22
@Azagaros The code works fine but I was looking more towards the user inputing the Roman Numbers and output what that roman number equals. EX: XXLVI = 56. You have it where the program already has is the input. Is there a way to change it to have the user input the roman numbers?
Last edited on
@Explicit
Here is the program that allows for user input.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````// Roman Numerals To Decimal.cpp : Defines the entry point for the console application. // #include "stdafx.h" // Used because I compile with MS Visual C++ 2008, and this is needed for it #include #include using namespace std; struct RomanNumber { public: string get() { return romanNumber; } void show() { cout << "Roman Number is " << romanNumber << endl; } RomanNumber(const string &input) { romanNumber = input; } int convert() { int length = romanNumber.length(); int previous = 0; bool error = false; int nIndex = 0; sum = 0; while( (error == false) && (nIndex < length) ) { switch(romanNumber[nIndex]) { case 'M': sum += 1000; if(previous < 1000) { sum -= 2 * previous; } previous = 1000; break; case 'D': sum += 500; if(previous < 500) { sum -= 2 * previous; } previous = 500; break; case 'C': sum += 100; if(previous < 100) { sum -= 2 * previous; } previous = 100; break; case 'L': sum += 50; if(previous < 50) { sum -= 2 * previous; } previous = 50; break; case 'X': sum += 10; if(previous < 10) { sum -= 2 * previous; } previous = 10; break; case 'V': sum += 5; if(previous < 5) { sum -= 2 * previous; } previous = 5; break; case 'I': sum += 1; if(previous < 1) { sum -= 2 * previous; } previous = 1; break; default: cout << romanNumber[nIndex] << " is not a Roman Numeral!" << endl; error = true; sum = 0; } // switch nIndex++; } // while return sum; } int length() { return romanNumber.length(); } private: string romanNumber; int sum; }; int main() { string myInput; int value; cout << "Please enter the Roman Numeral to convert : "; cin >> myInput; RomanNumber myRomanNumber(myInput); value = myRomanNumber.convert(); cout << "Roman Number " << myInput << " equals " << value <

I'm not sure how much this matters, but...something like "XXLVI" is not a valid Roman numeral. That value should be expressed "XXXVI."

How strictly you want to adhere to proper Roman numerology will influence your parsing rules.
Topic archived. No new replies allowed.