Roman Numeral Converter Program

In summary: The value of M is " << M << "\n"; cout << "\nThe value of D is " << D << "\n"; cout << "\nThe value of C is " << C << "\n"; cout << "\nThe value of L is " << L << "\n"; cout << "\nThe value of X is " << X << "\n"; cout << "\nThe value of IX is " << IX << "\n";
  • #1
Marcin H
306
6

Homework Statement


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!

Homework Equations


None

The Attempt at a Solution


Code:
#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";
       
        cout<< "\n\t Please enter your number:\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;
}
 
Physics news on Phys.org
  • #2
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
SteamKing said:
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?
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
Marcin H said:
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.
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
SteamKing said:
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.
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:
else
{
       if (number = 1000)
       (then what...?)

       if (number = 500)
       (then what...?)
 
  • #6
Marcin H said:
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:
else
{
       if (number = 1000)
       (then what...?)

       if (number = 500)
       (then what...?)
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
SteamKing said:
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.
I'm not sure about that. Would I have to use strings somehow?
 
  • #8
Marcin H said:
I'm not sure about that. Would I have to use strings somehow?
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
SteamKing said:
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.
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
Marcin H said:
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
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
SteamKing said:
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.
Yeah, that's my problem. I have no idea how I would start that.
 
  • #12
Marcin H said:
Yeah, that's my problem. I have no idea how I would start that.
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
SteamKing said:
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.
I don't have a textbook for the class. I just need some help starting the program.
 
  • #14
Marcin H said:
3957 would be MMMDCCCCLVII
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.

Marcin H said:
I don't have a textbook for the class.
That's not so good. What do you use for a reference and explanation of concepts?
 
  • #15
Mark44 said:
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?
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
Mark44 said:
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.

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
Marcin H said:
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...
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:
    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
Mark44 said:
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:
    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.
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
Marcin H said:
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?
An example that has a loop and a branch? Doesn't the instructor of your class show you examples of these control structures?
Marcin H said:
the program for roman numerals from 1-10?
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
Mark44 said:
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.
*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.
 

What is a Roman Numeral Converter Program?

A Roman Numeral Converter Program is a computer program that allows users to convert numbers from the Arabic numeral system (1, 2, 3, etc.) to the Roman numeral system (I, II, III, etc.) and vice versa.

How does a Roman Numeral Converter Program work?

A Roman Numeral Converter Program typically uses a set of rules and algorithms to convert numbers between the two numeral systems. For example, the program may recognize that the Roman numeral for 4 is IV and use this rule to convert any number that contains 4 (e.g. 14, 24, etc.).

Can a Roman Numeral Converter Program handle all numbers?

Most Roman Numeral Converter Programs have a limit to the numbers they can convert, typically up to a few thousand. This is because the Roman numeral system does not have a symbol for large numbers like the Arabic system does. Some programs may also have a limit due to technical constraints.

What are the benefits of using a Roman Numeral Converter Program?

A Roman Numeral Converter Program can save time and effort for those who need to convert numbers between the two numeral systems frequently. It also helps with accuracy, as manually converting numbers can lead to errors.

How accurate is a Roman Numeral Converter Program?

A well-designed and regularly updated Roman Numeral Converter Program should be highly accurate in converting numbers between the two systems. However, it is always recommended to double-check the results to ensure accuracy.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
988
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
Back
Top