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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include "roman.h"
#include <string>

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string>

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 <<endl;
}


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 <iostream>
#include <string>

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 <<endl;
	
}

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.