# Roman Numeral Converter Program

1. Oct 12, 2015

### Marcin H

1. The problem statement, all variables and given/known data
Make a program that converts numbers into roman numerals. Use strings, looping, and branching. The code posted is what I have so far, but I have no idea how to continue this program. I'm not sure if what I have so far is right or will even work or if I can use it at all. Can someone help me finish this program and explain in detail what each part does. Each time my instructor explains something to me, I have no idea what he is saying to me. This is my first time programming so please help! Thank you!!!!

2. Relevant equations
None

3. The attempt at a solution
Code (C):
#include<iostream>
#include<climits>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<string>

using namespace std;

int main(void)

{
string romnum;
unsigned short number;
short M;  // Numerical Value 1000
short D;  // Numerical Value 500
short C;  // Numerical Value 100
short L;  // Numerical Value 50
short X;  // Numerical Value 10
short IX; // Numerical Value 9
short V;  // Numerical Value 5
short IV; // Numerical Value 4
short I; // Numerical Value 1

char yes_no

cout << "\nWould you like to convert numbers into Roman Numerals?";
cin >> yes_no;
cin.ignore(INT_MAX, '\n');
while ( toupper(yes_no) == 'Y' )

{

cout<< "\n\t Excelent! Let's get started!\n";

cin >> number;
if ( number >= 4000 || number <= 0 )
{
cout << "\nError.  Please enter a number greater than 0 and less than
4000.\n"
;

}
else
{

M = number / 1000;
number = number % 1000;

D = number / 500;
number = number % 500;

C = number / 100;
number = number % 100;

L = number / 50;
number = number % 50;

X = number / 10;
number = number % 10;

IX = number / 9;
number = number % 9;

V = number / 5;
number = number % 5;

IV = number / 4;
number = number % 4;

I = number;

cout << "Thank you.\n";
}

}

cout<<"\n\t\tThank you for using the Roman Numeral Converter!!!\n";

cout << "\n\t\t\t\tHAVE A GREAT DAY!!!\n";
}

return 0;
}

2. Oct 12, 2015

### SteamKing

Staff Emeritus
Well, are you familiar with how Roman numerals work? Do you know they're different from place-value numbers like base-10 or decimals?

If you had to convert 3957 to Roman numerals, what would the result be?

3. Oct 12, 2015

### Marcin H

I am familiar with how they work, I just don't know how to make a program out of it. For this program I understand everything up until the first else statement. Everything after that doesn't make too much sense to me and I don't know if I'm even supposed to use that or how to even use it. Honestly, I just found that online and thought I'd be able to do something with it. Sorry for the rant, but I've never been this confused/lost in a class before to the point where I have no idea how to write a simple program like this. It's very frustrating. My instructor doesn't teach that well, so it's all confusing to me.

4. Oct 12, 2015

### SteamKing

Staff Emeritus
Once you read in the user's input number, you check to make sure it's less than 4000. Users are known to ignore warnings, so the programmer checks input to make sure the program isn't going to blow up.

After the else, you establish the decimal values of the various Roman numerals: M = 1000, D = 500, etc. Once these values are established, you need to write some programming which will take your input number to be converted and decompose it into pieces parts which are compatible with expression in Roman numerals. That's why I asked how you would convert an arbitrary number like 3957 into Roman numerals. Make yourself a flow chart of how you would accomplish this process using pencil and paper, and then see if you can convert this to the necessary program steps. If you are not familiar with programming in C, I can't help you there.

5. Oct 12, 2015

### Marcin H

Sorry. Missed that question. 3957 would be MMMDCCCCLVII. And can you show me the first step how I could start this program, after the else?
Would I do something like:
Code (C):
else
{
if (number = 1000)
(then what...?)

if (number = 500)
(then what...?)

6. Oct 12, 2015

### SteamKing

Staff Emeritus
How would you determine that 3957 > 1000?

Remember, you're a computer and you don't know anything about Roman numerals except what your programming tells you.

7. Oct 12, 2015

### Marcin H

I'm not sure about that. Would I have to use strings somehow?

8. Oct 12, 2015

### SteamKing

Staff Emeritus
Not necessarily. This is a simple numerical comparison, analogous to figuring out if 3957 < 4000.

You're making this a more complex a task than it should be. Forget about the program for a minute. If you have only a pencil and a piece of paper, how would you go about step-by-step figuring out how to turn 3957 into MMMDCCCCLVII? You can use addition, subtraction, multiplication, and division of integers. You can also test numbers to see if they are equal to, less than, or greater than one another.

9. Oct 12, 2015

### Marcin H

well the way I thought about it was, how many thousands ( or M's) fit into 3957. Answer 3. Then how many 500's (or D's) fit into 957. And so on. Is that the wrong way of thinking about it? Also my instructor said we should use strings for this program.

1000 + 1000 + 1000 + 500 + 100 + 100 + 100 + 100 + 50 + 5 + 1 + 1
M_______ M______M_____D_____C_____C_____C______C____L____V___I___I

10. Oct 12, 2015

### SteamKing

Staff Emeritus
That's a perfectly valid approach to this problem. You can also use loops to help figure out, for example, how many 1000s are in the number to be converted, if you don't want to use division.

Strings are a must to store the Roman numeral, since it uses letters rather than numerals to represent a number.

11. Oct 12, 2015

### Marcin H

Yeah, that's my problem. I have no idea how I would start that.

12. Oct 13, 2015

### SteamKing

Staff Emeritus
You mean using strings to store the Roman numeral you develop?

You can't do much with strings except assign one or more letters to a string or concatenate one or more letters to an existing string.

You should study up on strings by reading the relevant sections in your language programming guide. The guide should discuss how you can manipulate strings or parts of strings, and what functions can be used to do this manipulation.

13. Oct 13, 2015

### Marcin H

I don't have a textbook for the class. I just need some help starting the program.

14. Oct 13, 2015

### Staff: Mentor

No, it wouldn't. Not only do Roman numerals add, but for some numbers, they subtract. For example, 9 is not VIIII -- it's IX. Instead of 9 being 5 + 4 (VIIII) it's 10 - 1 (written IX). Similarly 90 is XC, and for 3900, it would be MMMCM, which is sort of like 3000 + 1000 - 100.

That's not so good. What do you use for a reference and explanation of concepts?

15. Oct 13, 2015

### Marcin H

notes and google :/ Do you know how I could apply that to my program. I just need an example of one branch and I think I could get the rest. Hopefully...

16. Oct 13, 2015

### SteamKing

Staff Emeritus
Like a lot of things Roman, there is some variation in how particular numbers may be written.

https://en.wikipedia.org/wiki/Roman_numerals

The modern way to write '4' in Roman numerals uses the subtractive form, 'IV', but the additive variation, IIII, often appears.

17. Oct 13, 2015

### Staff: Mentor

Try writing a program with input limited to numbers between 1 and 10. Once you have that working, extend it to numbers between 1 and 100. Work your way up to 4000.

You should NOT have these variables:
Code (Text):

short M;  // Numerical Value 1000
short D;  // Numerical Value 500
short C;  // Numerical Value 100
short L;  // Numerical Value 50
short X;  // Numerical Value 10
short IX; // Numerical Value 9
short V;  // Numerical Value 5
short IV; // Numerical Value 4
short I; // Numerical Value 1

Your program should print these characters, so there is no sense in making variables of them.

18. Oct 13, 2015

### Marcin H

I don't know how to do that either. I was just introduced to looping and branching. Can you show me an example of that? the program for roman numerals from 1-10?

19. Oct 13, 2015

### Staff: Mentor

An example that has a loop and a branch? Doesn't the instructor of your class show you examples of these control structures?
I won't show you a program, but I'l show you an algorithm that maybe you can translate into C code.

Prompt user for an input value num // Assume that 1 <= num <= 10
if num == 1 print "I"
else if num == 2 print "II"
else if num == 3 print "III"
else if num = 4 print "IV"
else if num == 5 print "V"
else if num = 6 print "VI"
else if num = 7 print "VII"
else if num = 8 print "VIII"
else if num = 9 print "IX"
else print "X"

For your program, you need to parse the number from the most significant digit down to the ones digit, to determine how many thousands there are (0, 1, 2, 3, or 4), how many hundreds (0, 1, 2, ..., 9), how many tens (0, 1, 2, ..., 9), and how many 1s. You need to handle special cases separately, like 4 (IV), 14 (XIV), and so on, as well as 9 (IX), 19 (XIX), and so on.

The variables you really need are not the ones you show above, but ones for the thousands digit in your number, the hundreds digit, the tens digit, and the ones digit.

20. Oct 13, 2015

### Marcin H

*sigh. Never mind. I still don't know how to make this program. I'll just try working on another one. Sorry for wasting your time.