A C++ program to convert roman numerals to English ones

Aug 21, 2010 at 11:40am
Hey, everyone. I wrote this C++ program to convert Roman Numerals into Ordinary English ones. It works fine - but I have a few questions.
1. Can it be made more efficient because i've used simple logic but the result is that the program looks so ugly - line 52 streches so long and that makes the code look a little confusing to read.
2. Can you peek more than one letter ahead because the program, as it is - can not tell that IIIX is 7 and NOT eleven because it only peeks one letter ahead.
3. What is the difference between formatted and unformatted input and does it make a difference if you use one and not the other?


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

int main()
{
    char roman_Numeral;
    int arabic_Numeral=0;

    cout<<"Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
    while(cin.get(roman_Numeral))
    {
                             if (roman_Numeral == 'M') arabic_Numeral=arabic_Numeral+1000;
                             
                             else if (roman_Numeral == 'D')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M'){ arabic_Numeral=arabic_Numeral-500; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+500; continue;}
                             }
                             
                             else if (roman_Numeral == 'C')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M' || roman_Numeral == 'D'){ arabic_Numeral=arabic_Numeral-100; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+100; continue;}
                             }
                             
                             else if (roman_Numeral == 'L')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M' || roman_Numeral == 'D' || roman_Numeral == 'C'){ arabic_Numeral=arabic_Numeral-50; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+50; continue;}
                             }                       
                             
                             else if (roman_Numeral == 'X')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M' || roman_Numeral == 'D' || roman_Numeral == 'C' || roman_Numeral == 'L'){ arabic_Numeral=arabic_Numeral-10; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+10; continue;}
                             }
                             
                             else if (roman_Numeral == 'V')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M' || roman_Numeral == 'D' || roman_Numeral == 'C' || roman_Numeral == 'L' || roman_Numeral == 'X'){ arabic_Numeral=arabic_Numeral-5; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+5; continue;}
                             }   
                             
                             else if (roman_Numeral == 'I')
                             {
                                                    roman_Numeral = cin.peek();
                                                    if(roman_Numeral == 'M' || roman_Numeral == 'D' || roman_Numeral == 'C' || roman_Numeral == 'L' || roman_Numeral == 'X' || roman_Numeral == 'V'){ arabic_Numeral=arabic_Numeral-1; continue;}
                                                    else{ arabic_Numeral=arabic_Numeral+1; continue;}
                             }
                             else break;
    }
cout<<arabic_Numeral<<endl;
return 0;
}

Aug 21, 2010 at 12:06pm
You might want to consider using the std::string find() function:

http://www.cplusplus.com/reference/string/string/find/

You can set up a string containing all the characters you want to test for and then simply see if your input character is contained in that string.

As each of your tests contains an increasing set of possibilities you can also make use of the offset feature of the find() function to include as many or as few of the characters as you need:

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

std::string numerals = "VXLCDM";

int main()
{
	char roman_Numeral;
	int arabic_Numeral = 0;

	cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
	while(cin.get(roman_Numeral))
	{
		if(roman_Numeral == 'M')
			arabic_Numeral = arabic_Numeral + 1000;

		else if(roman_Numeral == 'D')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral, 5) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 500;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 500;
				continue;
			}
		}

		else if(roman_Numeral == 'C')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral, 4) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 100;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 100;
				continue;
			}
		}

		else if(roman_Numeral == 'L')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral, 3) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 50;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 50;
				continue;
			}
		}

		else if(roman_Numeral == 'X')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral, 2) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 10;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 10;
				continue;
			}
		}

		else if(roman_Numeral == 'V')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral, 1) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 5;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 5;
				continue;
			}
		}

		else if(roman_Numeral == 'I')
		{
			roman_Numeral = cin.peek();
			if(numerals.find(roman_Numeral) != std::string::npos)
			{
				arabic_Numeral = arabic_Numeral - 1;
				continue;
			}
			else
			{
				arabic_Numeral = arabic_Numeral + 1;
				continue;
			}
		}
		else
			break;
	}
	cout << arabic_Numeral << endl;
	return 0;
}
Aug 21, 2010 at 12:10pm
I suggest to use std::getline(std::cin, some_str);. With this you can read a whole line, and read ahead as much as you want. Do some searching on Google for , this algorithm could be shortened a lot.

BTW, English numbers, are called Arabic numbers
Aug 22, 2010 at 12:02am
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

#include<iostream.h>
#include<conio.h>

	char input[10];
	int  angka[10];
	int  i;
void convert()
{
	while(input[i]!=NULL)
	{
		if(input[i]== 'I')
		  angka[i]=1;

		  if(input[i]== 'V')
		  angka[i]=5;

		  if(input[i]== 'X')
		  angka[i]=10;

		  if(input[i]== 'L')
		  angka[i]=50;

		  if(input[i]== 'C')
		  angka[i]=100;

		  if(input[i]== 'D')
		  angka[i]=500;

		  if(input[i]== 'M')
		  angka[i]=1000;




		  i++;

	}
}

void main()
{
	mulai:
	  int n = 0,total=0,x=0,totals;
	  i=0;
	  cout<<"Input Roma Number : ";
	  cin>>input;
	  convert();
		while(angka[n]!=NULL)
		{
			 if(angka[n]>=angka[n+1])
			 {
				 total=total+angka[n];

			 }
			 else
			 if(angka[n]<angka[n+1])
				{
				  x=angka[n+1]-angka[n];
				  n++;


				}
				 totals=total+x;
				n++;

		}

		cout<<totals<<endl;

}


this code convert roman to arabic
Topic archived. No new replies allowed.