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
70
  #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;
}
Here you go, this is what you COULD do.

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

const int I = 1;       
const int V = 5;
const int X = 10;
const int L = 50;
const int C = 100;
const int D = 500;       
const int M = 1000;
const int bad_val = -1; 
 
     
// This function Takes the number and changes it into
// a Roman Numeral.
char Romandigit ( int val )
{
   char digit_char;
   if ( val == I )
     digit_char = 'I';
   if ( val == V )
     digit_char = 'V';
   if ( val == X )
     digit_char = 'X';
   if ( val == L )
     digit_char = 'L';
   if ( val == C )
     digit_char = 'C';
   if ( val == D )
     digit_char = 'D';
   if ( val == M )
     digit_char = 'M';
  return digit_char;
}

//This function Takes the Roman Numeral and changes it into a 
//number and it also tests to find if a bad value is 
//present.
int Roman_digit_Value( char roman_digit )
{
   int val;
   if ( roman_digit == 'I' )
      val = I;
   else if ( roman_digit == 'V' )
      val = V;
   else if ( roman_digit == 'X' )
      val = X;
   else if ( roman_digit == 'L' )
      val = L;
   else if ( roman_digit == 'C' )
      val = C;
   else if ( roman_digit == 'D' )
      val = D;
   else if ( roman_digit == 'M' )
      val = M;
   else 
       val = bad_val;
   return val;
}

//This function prints the roman numeral as it reads the smallest
//amount of Roman Numerals possible.
void Print_Roman_Numeral ( int val )
{
   int number = val;
   int high_digit = M;
      while ( number > 0 )
      {
      if ( number > high_digit )
         cout << Roman_digit_Value ( high_digit );
         number = number - high_digit;
      if ( number > V )
         cout << Roman_digit_Value ( V );
         number = number - V;
      if ( number > X )
         cout << Roman_digit_Value ( X );
         number = number - X;
      if ( number > L )
         cout << Roman_digit_Value ( L );
         number = number - L;
      if ( number > C )
         cout << Roman_digit_Value ( C );
         number = number - C;
      if ( number > D )
         cout << Roman_digit_Value ( D );
         number = number - D;
      if ( number > M )
         cout << Roman_digit_Value ( M );
         number = number - M;
      }
}
//This function reads the input from the input file
//and then changes it to values.
void Print_Result ( int Roman_Num, int Second_Roman_Num, int Result, char operation )
{
   
   if ( operation == '+' )
      Print_Roman_Numeral ( Result );
      cout << "The sum of " << Roman_Num << " and " << Second_Roman_Num << 
      " is " << "(" << Result << ")" << endl; 
   if ( operation == '-' )
      cout << "The difference of " << Roman_Num << " and " << Second_Roman_Num << 
      " is " << "(" << Result << ")"; 
   if ( operation == '/' )
      cout << "The quotient of " << Roman_Num << " and " << Second_Roman_Num << 
      " is " << "(" << Result << ")";
   if ( operation == '*' )
      cout << "The quotient of " << Roman_Num << " and " << Second_Roman_Num << 
      " is " << "(" << Result << ")"; 
}
//This function turns the two romman numeral numbers
//into one by doing the specified operation.
int Return_Operation ( int Roman_Num, int Second_Roman_Num, char operation )
{
   int Result = 0;
   if ( operation == '+' )
      Result = Roman_Num + Second_Roman_Num;
   if ( operation == '-' )
      Result = Roman_Num - Second_Roman_Num;
   if ( operation == '/' )
      Result = Roman_Num / Second_Roman_Num;
   if ( operation == '*' )
      Result = Roman_Num * Second_Roman_Num;
   return Result;
}      
//This function takes the roman digits thet were inputted
//and then changes them to a number value and it 
//determines weaher the value is bad.
int Get_Roman_Numeral()
{
   int num = 0;
   char ch = 0;
   int count = 1;
  
   while (count <= 7 ) 
   {
   cin >> ch;
 
   int value = Roman_digit_Value(ch);
       num = num + value;
   count++;
   }
   return num;  
}
       
int main()
{ 
  char operation = '+';
  string input_Roman;  
  int Roman_Num = 0;
  int Second_Roman_Num = 0;
  int Result = 0;
  Roman_Num = Get_Roman_Numeral();
      while ( !cin.eof() ) 
      {   
         cout << "The first number is " << Roman_Num << endl;
         Second_Roman_Num = Get_Roman_Numeral();
         cout << "The second number is " << Second_Roman_Num << endl;
         cin >> operation;
         cout << "Arithmetic operation is " << operation << endl;
         Result = Return_Operation (Second_Roman_Num, Roman_Num, operation);
         Print_Result( Roman_Num, Second_Roman_Num, Result, operation );
         Roman_Num = Get_Roman_Numeral();

      }
      return 0;
}

Last edited on
@ProgrammerXYZ

Your main problem in the Roman Numeral to decimal conversion program, is you're not checking if a number is to be subtracted from a larger number. If the value of a Roman Numeral is lower than the next value, that value gets subtracted. You are just checking the values, and adding.

Value .. MCMLXXVIII
M = 1000
C is less than the next M, so 100 is subtracted from the 1000 = 900
L = 50
X = 10
X = 10
V = 5
I = 1
I = 1
I = 1

Total = 1978
Yeah I know, but I tried everything, and the program doesnt do what its supposed to. The value doesnt come out to 1978. I dont know how to check if the number is larger than the previous number using substrings and void function, as that is what I am supposed to do
@C0D3FR3AK

Your idea works, but the math ends up being wrong and thats the main problem. Thanks though
Topic archived. No new replies allowed.