Integer to Roman Numerals

I have a homework assignment to do the following.

Your task is to write a C++ program to help you convert a number into roman numerals. Roman numerals are I for 1, V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1000. Some numbers are formed by using a subtraction of Roman “digits”; for example, IV is 4, since V minus I is 4. Others are IX
for 9, XL for 40, XC for 90, CD for 400, and CM for 900. Your program will proceed in three parts:

1. Write a program that reads in a decimal number from the screen (using cin) and prints out
a simple Roman numeral. A simple Roman numeral does not use the subtraction rule. For
example, the simple Roman numeral for the number 493 will be CCCCLXXXXIII, and the simple
Roman numeral for the number 3896 will be MMMDCCCLXXXXVI. Your program should print the
entire Roman numeral on a single line, with no spaces between letters. You will need a while
loop in your program to print out the letters one at a time. Hint: Look at your minutes
program for ideas on how figure out which letter to print next.

2. Modify your program so that you print out all of the repeated letters in a single iteration
of your while loop. You will need to put a few for loops inside your while loop to do this
part. For example, if the Roman numeral you have to print out is MMMMXXX, the first part
iterated through your while loop seven times, one for each letter you were printing out. Now,
you want to iterate your while loop just twice, one for each kind of Roman numeral being
printed. Thus, the first iteration will print out MMMM, and the second will print out XXX. Your
final output will be MMMMXXX on a single line.

3. Modify your program to also handle subtractions. So now, 493 should give the answer CDXCIII,and 3896 should give the answer MMMDCCCXCVI.


I got the program to work with this 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

#include <iostream>

using namespace std;

int main()
{

double num;
int intnum, m, d, c, l, x, v, i, n;
char yes ='y';

while (yes == 'y')
{
cout << "Enter a number: ";
cin >> num;
intnum = (int)num;

if (intnum >= 1000)
{  
    m = intnum / 1000;
    n = 0;
		{
		for (n; n < m; n++)
		cout << "M";
		}
	intnum = intnum%1000;
}

if (intnum >= 900)
{
	cout << "CM";
	intnum = intnum%900;
}
	else if (intnum >= 500)
    {
			{
			d = intnum / 500;
			n = 0;
			for (n; n < d; n++)
			cout << "D";
			}
        intnum = intnum%500;
    }
       
if (intnum >= 400)
{
	cout << "CD";
	intnum = intnum%400;
}       
    else if (intnum >= 100)
	{
		    {
            c = intnum / 100;
            n = 0;
            for (n; n < c; n++)
            cout << "C";
			}
		intnum = intnum%100;
	}

if (intnum >= 90)
{
cout << "XC";
intnum = intnum%90;
}

	else if (intnum >= 50)
	{
			{
            l = intnum / 50;
			n = 0;
            for (n; n < l; n++)
            cout << "L";
			}
		intnum = intnum%50;
	}
if (intnum >= 40)
{
cout << "XL";
intnum = intnum%40;
}
       
	else if (intnum >= 10)
	{
			{
            x = intnum / 10;
            n = 0;
            for (n; n < x; n++)
            cout << "X";
			}
		intnum = intnum%10;
	}

if (intnum >= 9)
{
cout << "IX";
intnum = intnum%9;
}

	else if (intnum >= 5)
	{
			{
            v = intnum / 5;
            n = 0;
            for (n; n < v; n++)
            cout << "V";
			}
		intnum = intnum%5;
	}
if (intnum >= 4)
{
cout << "IV";
intnum = intnum%4;
}
	else if (intnum >= 1)
	{
           i = intnum;
           n = 0;
           for (n; n < i; n++)
           cout << "I";
	}
	    cout << "\nWould you like to run this program again? (y/n): ";
	    cin >> yes;
        cout << endl; 
}
return 0;
}


However, I don't quite understand the procedure that the directions tell me to do. For instance, number one says:

"You will need a while loop in your program to print out the letters one at a time."

Don't I need more than one like I do?

I also don't understand what number 2 means.

Can anyone help clarify some of this for me?
Thanks

Last edited on
I messed around with it a little this morning and got this for number one
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

#include <iostream>

using namespace std;

int main()
{

double num;
int intnum, m, d, c, l, x, v, i, n;



cout << "Enter a number:";
cin >> num;
intnum = (int)num;

m = intnum / 1000;
d = ((intnum%1000)/500);
c = ((intnum%500)/100);
l = ((intnum%100)/50);
x = ((intnum%50)/10);
v = ((intnum%10)/5);
i = (intnum%5);
n = m+d+c+l+x+v+i;



while (n > 0)
{
      cout << "!";// to show the loop ran everytime
      
      if (m > 0)
      {
      cout << "M";
      m--;
      n--;
      }

      else if (d > 0)
      {
      cout << "D";
      d--;
      n--;
      }
      else if (c > 0)
      {
      cout << "C";
      c--;
      n--;
      }
      else if (l > 0)
      {
      cout << "L";
      l--;
      n--;
      }
      else if (x > 0)
      {
      cout << "X";
      x--;
      n--;
      }
     else if (v > 0)
      {
      cout << "V";
      v--;
      n--;
      }
     else if (i > 0)
      {
      cout << "I";
      i--;
      n--;
      }

}


system("pause");
return 0;
}

Enter a number:5678
!M!M!M!M!M!D!C!L!X!X!V!I!I!I
Press any key to continue . . .


That is running the while loop 7 times like the directions say.

I then tried to do number 2. But the loop is completing everything in 1 run.
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

#include <iostream>

using namespace std;

int main()
{

double num;
int intnum, m, d, c, l, x, v, i, n;



cout << "Enter a number:";
cin >> num;
intnum = (int)num;

m = intnum / 1000;
d = ((intnum%1000)/500);
c = ((intnum%500)/100);
l = ((intnum%100)/50);
x = ((intnum%50)/10);
v = ((intnum%10)/5);
i = (intnum%5);
n = m+d+c+l+x+v+i;


while (n > 0)
{
      cout << "!"; // to show the loop running
      {
      for (m; m>0; m--)
		cout << "M";
      }
      {
      for (d; d>0; d--)
		cout << "D";
      }
            {
      for (c; c>0; c--)
		cout << "C";
      }
      {
      for (l; l>0; l--)
		cout << "L";
      }
      {
      for (x; x>0; x--)
		cout << "X";
      }
      {
      for (v; v>0; v--)
		cout << "V";
      }
      {
      for (i; i>0; i--)
		cout << "I";
      }
      n--;


}


system("pause");
return 0;
}


Output
Enter a number:5678
!MMMMMDCLXXVIII!!!!!!!!!!!!!
Press any key to continue . . .
Last edited on
Topic archived. No new replies allowed.