Roman Numeral program

Hello to anyone! thank you for any help!

Write a program that accepts a year written as a four-digit Arabic ( ordinary) number and outputs the year written in Roman numerals. Important Roman Numbers are I for 1,V for 5, X for 10, L for 50, C for 100, D for 500 and M for 1000. Recall that some numbers are formed by using a kind of subtraction of one Roman 'digit' ; for example , IV is 4 produced as V minus 1. and XL is 40, CM is 900 and so on. A few sample years: MCM is 1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940, MCMLXXXIX is 1989, Assume the year that the user inputs is between 1000 and 3000. Your program will include a loop that lets the user repeat this calculation until the user says they are done.



hints : module operation % may help and switch statements may help,

Hint: do we need a statement at the bottom of the program that says Run again ( y = YES)? Or can you figure out a interesting way to end the program?




What I've done:

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
int year;
int roman;

cout << "Please enter a year: ";
cin >> year;

while (year > 1000 && year < 3000)
{
switch (year < 1000)
{
case 1:
roman += 'M';
break;
case 2:
roman += 'MM';
break;
case 3:
roman += 'MMM';
break;

default:
break;
}


}


}


Thanks you for any help Im trying to utilize the switch statement per Professors request. I understand the basic concept of the switch statement but need assistance executing the program
Last edited on
Consider some helper functions.

1
2
3
4
5
6
string roman;
int year = 1960;  // prompt for this
roman += encodeThousands((year / 1000) % 10));  // a repeated sequence of 'M'
roman += encodeHundreds((year / 100) % 10));  // some sequence of C, with D and M at 400 and 900
roman += encodeTens((year / 10) % 10)); // some sequence of X, with L and C at 40 and 90
roman += encodeUnits((year / 1) % 10)); // some sequence of I, with V and X at 4 and 9 
Here a possible solution in some kind of pseudo-code (it is REXX):
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
/* RONUM.RX: convert decimal 1..3000 to roman numerals */
arg denum .
select
  when 1 - datatype(denum, 'W') then exit 9   /* arg not an integer */
  when denum < 1,
     | denum > 3000 then exit 8               /* outside range */
  otherwise                                   /* input is 1..3000 */

  rn.   = 'NaRN'; rval.  = 0     /* default */
  rn.1  = 'M';    rval.1 = 1000  /* guess what */
  rn.2  = 'CM';   rval.2 = 900
  rn.3  = 'D';    rval.3 = 500
  rn.4  = 'CD';   rval.4 = 400
  rn.5  = 'C';    rval.5 = 100
  rn.6  = 'XC';   rval.6 = 90
  rn.7  = 'L';    rval.7 = 50
  rn.8  = 'XL';   rval.8 = 40
  rn.9  = 'X';    rval.9 = 10
  rn.10 = 'IX';  rval.10 = 9
  rn.11 = 'V';   rval.11 = 5
  rn.12 = 'IV';  rval.12 = 4
  rn.13 = 'I';   rval.13 = 1
  rn.0 = 13;     rval.0  = 13     /* # make stem some kind of array */

  ronum = ''
  do i = 1 to rn.0 while denum > 0
    if rval.i <= denum then do until denum < rval.i
      ronum = ronum || rn.i
      denum = denum - rval.i
    end
  end

  say ronum

end

The two arrays are the essential part: take combined symbols into account. Not my idea, found it elsewhere.
@dipiet,
Find the number of thousands, hundreds, tends, units, by successive use of integer division (/) and modulo or remainder operator (%), similar to @salem c's suggestion.

Then just use look-up tables of string arrays. e.g. for the hundreds:
const string hu[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
to
string roman = th[thousands] + hu[hundreds] + te[tens] + un[units];

There are plenty of other ways, but this is straightforward and simple.
Last edited on
Topic archived. No new replies allowed.