Not converting to Roman Numeral Correctly.

Basically I'm supposed to write a program that uses class/inheritance to convert an integer to roman numeral to integer and back to roman numeral. However when I try to print out the integer. It will not print out the correct integer. Also my program seems to add two extra 'I' when you input a number like 1992

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <string>
using namespace std;

class Romantype {
private:
	int decimal = 0;
	string romannumeral;

public:
	string getRomanNumeral() {
		return romannumeral;

	}
	void SetRomanNumeral(string str1) {
		romannumeral = str1;
	}
	void resetDecimal() {
		decimal = 0;
	}
	void showDecimal() {
		cout << "The decimal number is : " << decimal;
	}
	void ConvertAndStoreDecimal() {
		for (char i = 0; i < romannumeral.length(); i++) {
			if (romannumeral[i] == 'M') {
				if (i > 0 && romannumeral[i - 1] == 'C') {
					decimal -= 200;
				}
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 1000;
			}
			if (romannumeral[i] == 'D') {
				if (i > 0 && romannumeral[i - 1] == 'C') {
					decimal -= 200;
				}
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 500;
			}
			if (romannumeral[i] == 'C') {
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 100;
			}
			if (romannumeral[i] == 'L') {
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 50;
			}
			if (romannumeral[i] == 'X') {
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 10;
			}
			if (romannumeral[i] == 'V') {
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 5;
			}
			if (romannumeral[i] == 'I') {
				decimal += 1;
			}
		}
	}

	void PrintRomanNumeral() {
		cout << endl << "The roman numeral entered is " << romannumeral;
	}
	void PrintRomanNumeralDecimal() {
		cout << "The roman numeral is " << romannumeral << endl;
		cout << "The decimal is " << decimal << endl;
	}
};

class ExtDecimalType: public Romantype {
private:
	int numero, leftover, og;
	string romano;
public:
	ExtDecimalType(int value) {
		numero = value;
	}
	void setDecimalNumber() {

		og = numero;

	}
	void ConvertAndStoreRomanNumeral() {
		if (numero >= 1000) {
			leftover = numero / 1000;
			for (int i = 0; i< leftover; i++) {
				romano += 'M';
			}
			numero %= 1000;
		}
		if (numero >= 100) {
			leftover = numero / 100;
			if (leftover == 9) {
				romano += "CM";
			}
			else if (leftover >= 5) {
				romano += 'D';
				for (int i = 0; i < leftover - 5; i++) {
					romano += 'C';
				}
			}
			else if (leftover == 4) {
				romano += "CD";

			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'C';
				}
			}
			numero %= 100;
		}
		if (numero >= 10) {
			leftover = numero / 10;
			if (leftover == 9) {
				romano += "XC";
			}
			else if (leftover >= 5) {
				romano += 'L';
				for (int i = 0; i < leftover; i++) {
					romano += 'X';
				}
			}
			else if (leftover == 4) {
				romano += "XL";
			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'X';
				}
			}
			numero %= 10;
		}
		if (numero >= 1) {
			leftover = numero;
			if (leftover == 9) {
				romano += "IX";
			}
			else if (leftover >= 5) {
				romano += 'V';
				for (int i = 0; i< leftover; i++) {
					romano += 'I';
				}
			}
			else if (leftover == 4) {
				romano += "IV";
			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'I';
				}
			}
		}
	}
	int getDecimal() {
		return numero;
	}
	void PrintDecimal() {
		cout << "The decimal number is " << og << endl;
	}
	void PrintRomanNumeral() {
		cout << "The roman numeral is " << romano << endl;
	}
	void PrintRomanNumeralDecimal() {
		cout << "The roman numeral is " << romano << endl;
		cout << "The decimal is " << og << endl;
	}
};

int main() {

	Romantype rt;
	string input;


	cout << "Please enter a Roman numeral : ";

	cin >> input;

	rt.SetRomanNumeral(input);

	rt.ConvertAndStoreDecimal(); // Convert to a Decimal and then store the Roman Numeral and the Decimal

	rt.PrintRomanNumeralDecimal(); // Print both Roman Numeral and Decimal

	int number;

	cout << "Please enter a decimal number : ";

	cin >> number;

	ExtDecimalType num(number);

	num.setDecimalNumber();

	num.ConvertAndStoreRomanNumeral(); // Convert to a Roman Numeral and then store the Roman Numeral and the Decimal

	num.PrintDecimal(); // Print only the Decimal 

	num.PrintRomanNumeral();  // Print only the Roman Numeral:: New - this was missing. Add on 3/30/2016

	num.SetRomanNumeral(num.getRomanNumeral());

	num.ConvertAndStoreRomanNumeral();  // Convert to a Decimal and then store the Roman Numeral and the Decimal

	num.PrintRomanNumeralDecimal(); // Print both Roman Numeral and Decimal

	return 0;
}
Last edited on
For example if you put in MCMXVII it is equivalent to 1917. But if you put 1917 my program converts it to something else.
@plsalinas

Here is a function to convert integers to roman numerals. Hopefully, you can use it in your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string Int_to_Roman(int calc)
{
	int     Integer_Values[13] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
	string Roman_Values[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
	string roman_num = "";
	int x = 0;
	for ( x = 0; x < 13; x++ )
	{
		do
		{
			if (calc >= Integer_Values[x])
			{
				calc -= Integer_Values[x];
				roman_num += Roman_Values[x];
			}
		} while (calc >= Integer_Values[x]);
	}

	return roman_num;
}
@whitenite1

I managed to change my code to get to calculate correctly. The issue I'm having now is that when I decide to use inheritance in this line.

 
num.SetRomanNumeral(num.getRomanNumeral());


It prints out the roman numeral with extra 'I's.

This is the updated code.

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <string>

using namespace std;

class Romantype {
private:
	int decimal = 0;
	string romannumeral, ruman;

public:
	string getRomanNumeral() {
		return ruman;
		
	}
	void SetRomanNumeral(string str1) {
		romannumeral = str1;
	}
	void resetDecimal() {
		decimal = 0;
	}
	void showDecimal() {
		cout << "The decimal number is : " << decimal;
	}
	void ConvertAndStoreDecimal() {
		for (char i = 0; i < romannumeral.length(); i++) {
			if (romannumeral[i] == 'M') {
				if (i > 0 && romannumeral[i - 1] == 'C') {
					decimal -= 200;
				}
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 1000;
			}
			if (romannumeral[i] == 'D') {
				if (i > 0 && romannumeral[i - 1] == 'C') {
					decimal -= 200;
				}
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 500;
			}
			if (romannumeral[i] == 'C') {
				if (i > 0 && romannumeral[i - 1] == 'L') {
					decimal -= 100;
				}
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 100;
			}
			if (romannumeral[i] == 'L') {
				if (i > 0 && romannumeral[i - 1] == 'X') {
					decimal -= 20;
				}
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 50;
			}
			if (romannumeral[i] == 'X') {
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 10;
			}
			if (romannumeral[i] == 'V') {
				if (i > 0 && romannumeral[i - 1] == 'I') {
					decimal -= 2;
				}
				decimal += 5;
			}
			if (romannumeral[i] == 'I') {
				decimal += 1;
			}
		}
		ruman = romannumeral;
	}

	void PrintRomanNumeral() {
		cout << endl << "The roman numeral entered is " << romannumeral;
	}
	void PrintRomanNumeralDecimal() {
		cout << "The roman numeral is " << romannumeral << endl;
		cout << "The decimal is " << decimal << endl;
	}
};

class ExtDecimalType: public Romantype {
private:
	int numero, leftover, og;
	string romano, sromano;
public:
	ExtDecimalType(int value) {
		numero = value;
	}
	void setDecimalNumber() {

		og = numero;

	}
	void ConvertAndStoreRomanNumeral() {
		if (numero >= 1000) {
			leftover = numero / 1000;
			for (int i = 0; i< leftover; i++) {
				romano += 'M';
			}
			numero %= 1000;
		}
		if (numero >= 100) {
			leftover = numero / 100;
			if (leftover == 9) {
				romano += "CM";
			}
			else if (leftover >= 5) {
				romano += 'D';
				for (int i = 0; i < leftover - 5; i++) {
					romano += 'C';
				}
			}
			else if (leftover == 4) {
				romano += "CD";

			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'C';
				}
			}
			numero %= 100;
		}
		if (numero >= 10) {
			leftover = numero / 10;
			if (leftover == 9) {
				romano += "XC";
			}
			else if (leftover >= 5) {
				romano += 'L';
				for (int i = 0; i < leftover; i++) {
					romano += 'X';
				}
			}
			else if (leftover == 4) {
				romano += "XL";
			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'X';
				}
			}
			numero %= 10;
		}
		if (numero >= 1) {
			leftover = numero;
			if (leftover == 9) {
				romano += "IX";
			}
			else if (leftover >= 5) {
				romano += 'V';
				for (int i = 0; i< leftover-5; i++) {
					romano += 'I';
				}
			}
			else if (leftover == 4) {
				romano += "IV";
			}
			else if (leftover >= 1) {
				for (int i = 0; i < leftover; i++) {
					romano += 'I';
				}
			}
		}
		sromano = romano;
	}
	int getDecimal() {
		return numero;
	}
	void PrintDecimal() {
		cout << "The decimal number is " << og << endl;
	}
	void PrintRomanNumeral() {
		cout << "The roman numeral is " << romano << endl;
	}
	void PrintRomanNumeralDecimal() {
		cout << "The roman numeral is " << romano << endl;
		cout << "The decimal is " << og << endl;
	}
};

int main() {

	Romantype rt;
	string input;
	

	cout << "Please enter a Roman numeral : ";

	cin >> input;

	rt.SetRomanNumeral(input);

	rt.ConvertAndStoreDecimal(); // Convert to a Decimal and then store the Roman Numeral and the Decimal

	rt.PrintRomanNumeralDecimal(); // Print both Roman Numeral and Decimal

	int number;

	cout << "Please enter a decimal number : ";

	cin >> number;

	ExtDecimalType num(number);

	num.setDecimalNumber();

	num.ConvertAndStoreRomanNumeral(); // Convert to a Roman Numeral and then store the Roman Numeral and the Decimal

	num.PrintDecimal(); // Print only the Decimal 

	num.PrintRomanNumeral();  // Print only the Roman Numeral:: New - this was missing. Add on 3/30/2016

	num.SetRomanNumeral(num.getRomanNumeral());

	num.ConvertAndStoreRomanNumeral();  // Convert to a Decimal and then store the Roman Numeral and the Decimal

	num.PrintRomanNumeralDecimal(); // Print both Roman Numeral and Decimal

	return 0;
}



Last edited on
@plsalinas

Sorry, have never used inheritance, in my recollection. Haven't really found a need for them, in my style of programming. Good luck on finding solutions.
Topic archived. No new replies allowed.