Conversion Calculator

I am supposed to be creating a program that converts roman numbers to its decimal values. I have done something, but the math is wrong. When entering MCMLXXVIII, I am supposed to get 1978, and instead I get 2178. I am using substrings to solve this problem, but I am not getting anywhere. Can somebody please advise me on what to do?

Thanks
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
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;

int main(){


	int M = 1000;
	int D = 500;
	int C = 100;
	int L = 50;
	int X = 10;
	int V = 5;
	int I = 1;
	

	int num = 0;

	cout << " Enter the Roman Numeral Value: ";
	string roman;
	cin >> roman;

	string sub = roman.substr(0, 2);
	cout << sub << endl;
	




	for (int i = 0; i < roman.length(); i++)
	{
		
		switch (roman.at(i))
		{
		
		case 'M':
		case 'm':
			num += M;
			break;
		case 'D':
		case 'd':
			num += D;
			break;
		case 'C':
		case 'c':
			num += C;
			break;
		case 'L':
		case 'l':
			num += L;
			break;
		case 'X':
		case 'x':
			num += X;
			break;
		case 'V':
		case 'v':
			num += V;
			break;
		case 'I':
		case 'i':
			num += I;
			break;
		}
	}

	cout << num << endl;
}
closed account (48bpfSEw)
you can not simply add the roman chiffres. There is a system behind:
http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

e.g.

IX is 9 and not 11
Last edited on
I know how they work. It's just that I dont know how to implement that using substrings, and functions
think of a dictionary( std::map ) that contains your roman number and their value are in decimal.
Topic archived. No new replies allowed.