How to make a roman numeral converter program

Pages: 12
I have to make a roman numeral program, but I have no idea how to start the looping and branching part. Can someone show me how to start that?

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
 #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
        {
             //(How do I start here???)
        }
        
        
    }
        
        



        cout<<"\n\t\tThank you for using the Roman Numeral Converter!!!\n";
        
        cout << "\n\t\t\t\tHAVE A GREAT DAY!!!\n";
    }
    
        return 0;
}
You left out that XL is 40, XC is 90, CD is 400, and CM is 900.
I would start off by writing a function that takes a roman numeral string and converts it to an integer. So, you should move all the roman numeral related code into a function above main like this:
1
2
3
4
5
int string int_from_roman(string romnum) {
    int num = 0;
    //iterate through romnum and add stuff to num along the way
    return num;
}

So the next question would be how do we know what to add to num while iterating through romnum?
I would suggest a lookup table of sorts:
1
2
string numerals[13] = {"IV", "IX", "XL", "XC", "CD", "CM", "I", "V", "X", "L", "C", "D", "M"};
int values[13] = {4, 9, 40, 90, 400, 900, 1, 5, 10, 50, 100, 500, 1000};

So now all you have to do is compare where you are in romnum with numerals and whatever matches first will tell you what to add to num. Cool!
Sorry. I didn't phrase my question right. I have to write a program that converts numbers into roman numerals. And i have to use strings, branching, and looping. I don't think i can use functions for this program.
Oops, I should have noticed from your code that you are trying to go from numbers to roman numerals and not the other way around. (I just assumed the opposite because who would even care what the roman numeral representation of a number is anymore?)

You can still use strings, branching, and looping even if you use functions... But do whatever your assignment requires... (Although learning good practice is better i.m.o.)

I would still suggest the same lookup table, but I would sort it from largest to smallest:
1
2
3
4
5
6
7
8
string roman_from_int(int num) {
    string numerals[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    string romnum = "";
    //continue to subtract the largest number from the lookup table and append
    //the corresponding string until num is 0
    return romnum;
}
I've never seen line 1 or line 7. I'm not sure if I could use those in my program, but could you explain what they do exactly. In line 1 you are declaring a string (like you would declare a datatype) but why did you put a curly bracket after? And what does return romnum do exactly. I've only used return 0 at the end of my programs. Sorry if these sound like dumb questions, but I am very new to this and having a hard time understanding. I'm learning more from forums and google than class unfortunately :/
Last edited on
Line 1 is a function declaration. I'm saying that the function roman_from_int takes an int and returns a string.
Here is a link to a small tutorial about functions:
http://www.cplusplus.com/doc/tutorial/functions/

And what does return romnum do exactly. I've only used return 0 at the end of my programs.
return /*anything*/; always exits the function and gives a value back to the function that called it. This is explained a bit more in the tutorial above.

If you want to do what I showed above without wrapping it in a function, just add some the inside stuff to main and make sure that an int num already has the value you want to convert inside it
1
2
3
4
5
6
string numerals[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string romnum = "";
//continue to subtract the largest number from the lookup table and append
//the corresponding string until num is 0
//now you can use romnum however you need to (display it, etc.) 
Hmm. I've never seen a program done this way before. So how would i write the function/equation to actually convert the number into roman numerals. (after line 3 in your code) Would i do something like...

1
2
3
4
if (int >= 1000)
cout<<"M";
elseif (int <= 900)
cout<<"CM";


I don't think that's right tho... I have no idea how I can make the program print roman numerals for something like 3975. How would i use the string to make the program do that correctly?
@BEARS

Here's how..

You can add this to a function that kevinkjt2000 told you about, or into your main program, that he also mentioned.

I wrote out the ideas of solving, but it's up to you to create it.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Use a for loop to go thru all 13 locations
	{
		do
		{
			// if your number >= values[x]
			{
				// Subtract the value of values[x] from your number
				// Add numerals[x] to existing romnum string;
			}
		} // while your number is higher than values[x];
	}

// Then return romnum string 
Last edited on
for lines 7 and 8, How would I do that exactly? Can you show me an example of one of if statements done correctly?
@BEARS

For line 8, it would be..

1
2
3
4
5
6
7
8
romnum = romnum + numerals[x];

// or you could write it as

romnum += numerals[x]; // Which means the same as the above example

// The use of 'x' is just me using the variable I used for my for loop
// Substitute the 'x' for whatever variable you're using for a loop 
Line 7 would be
number = number - values[x]
or
number -= values[x]

I find that often beginners neglect to realize that any complicated expression can go just about anywhere in c++.
For example if I want a gigantic formula to be computed for an array index, it is perfectly okay to do this:
array_name[5 + floor(sqrt(abs(x)/4.5)) + another_array[42 + b*3 - c]]
so long as 5 + floor(sqrt(abs(x)/4.5)) + another_array[42 + b*3 - c] is an integer it would make sense to the compiler.
The same rule goes for
variable = blah blah blah blah blah blah;
As long as blah blah blah blah blah blah evaluates to be variable's type
what is values[x] exactly? What would I write there? Would number be the integer the user enters and value[x] be the fixed number. So would this be how to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
cin >> number;
        if ( number >= 4000 || number <= 0 )
        {
            cout << "\nError.  Please enter a number greater than 0 and less than
                    4000.\n";
        
        }
        else
              {
               if(num >= 1000) //num is what user enters
               num = num - 1000
               romnum = romnum + M  //I'm not sure about this line.  Is that right?
               }


And then would I do the same thing for CM, D, CD, C, etc.?
Last edited on
BEARS wrote:
what is values[x] exactly?

Hint: It has to do with one of the three variables I posted earlier...
1
2
3
string numerals[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string romnum = "";

Yes number is the number that the user wishes to convert.

Try taking another look at what whitenite1 posted.
You want to "loop" across all 13 spots in the arrays. That way you do not have to write 13 if statements that all essentially do the same thing. Actually if statements are not even enough. What if the user enter 3687, 1000 needs to be subtracted three times...
Ok. So this is what i got. It compiles and runs, but it's not working correctly. Any idea how I can fix it? When I put it a number like 42, it rounds it to 40, saying the answer is "XL" in roman numerals. Or entering 56 gives me just "L". Also when i do enter a value, why doesn't the prgram go to the cout and deisplay thank you right away. You have to hit enter again for some reason.

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
130
131
132
133
134
#include<iostream>
#include<climits>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<string>

using namespace std;

string roman_from_int(short num);

int main(void)

{
    short num;
    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 >> num;
        if ( num >= 4000 && num <= 0 )
        {
            cout << "\nError.  Please enter a number greater than 0 and less than 4000.\n";
        }
        else
        {
        
            string roman = roman_from_int(num);
            cout << "roman form is " << roman << ".\n";

        } 
            
            
            

        cin >> yes_no;
        cin.ignore(INT_MAX, '\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;
}

string roman_from_int(short num) 
{
    string romnum = "";
    {
    if(num >= 1000)
    {
        num = num - 1000;
        romnum = romnum + "M";
    }   
    else if (num >= 900)
    {
        num = num - 900;
        romnum = romnum + "CD";
    }
    else if (num >= 500)
    {
        num = num - 500;
        romnum = romnum + "D";
    }
    else if (num >= 400)
    {
        num = num - 400;
        romnum = romnum + "CD";
    }   
    else if (num >= 100)
    {
        num = num - 100;
        romnum = romnum + "C";
    }
    else if (num >= 90)
    {
        num = num - 90;
        romnum = romnum + "XC";
    } 
    else if (num >= 50)
    {
        num = num - 50;
        romnum = romnum + "L";
    }
    else if (num >= 40)
    {
        num = num - 40;
        romnum = romnum + "XL";
    }
    else if (num >= 10)
    {
        num = num - 10;
        romnum = romnum + "X";
    }
    
    else if (num >= 9)
    {
        num = num - 9;
        romnum = romnum + "IX";
    }
    else if (num >= 5)
    {
        num = num - 5;
        romnum = romnum + "V";
    }
    else if (num >= 4)
    {
        num = num - 4;
        romnum = romnum + "IV";
    }
    else if (num >= 1)
    {
        num = num - 1;
        romnum = romnum + "I";
    }
    }

    return romnum;
}
Last edited on
BEARS wrote:
It compiles and runs, but it's not working correctly. Any idea how I can fix it?

Are you reading all of the advice that I have shared with you?
me wrote:
Actually if statements are not even enough. What if the user enter 3687, 1000 needs to be subtracted three times...

Your program needs to loop!
Something like:
1
2
3
4
5
6
7
8
9
10
//loop across all 13 possible numeral values (hint: for x 0 to 13-1)
{
    //continue to subtract from num and append to romnum...
    //until num is less than the current numeral value being processed
    //hint: while num is greater than or equal to values[x]
    {
        //subtract from num
        //append to romnum
    }
}

If you do not loop across all 13 possible numeral values, feel free to code 13 identical looking while loops instead...

BEARS wrote:
Also when i do enter a value, why doesn't the prgram go to the cout and deisplay thank you right away. You have to hit enter again for some reason.

Lines 48 and 49 are the culprits here.

Off topic: I do like the "HAVE A GREAT DAY!!!" message :)
Last edited on
So I would use a while statement after line 60? Im not sure why what I have now isn't working. How would I loop across all 13 possible values?
Last edited on
I just noticed that on line 72 you have CD instead of CM.

Say that you for some reason needed the number 3687 in roman numeral format. What would be the first thing that you would do?
I would think of the highest possible numeral that I can subtract from it which is M=1000, and I would repeat this process on paper (or in my head if the number is small) until I could not subtract any numerals.
 3697
-1000
-----
 2697    M
-1000
-----
 1697    MM
-1000
-----
  697    MMM
- 500   (at this point in my head I skipped 900 because it was too big)
-----
  197    MMMD
- 100   (I skipped 400 here)
-----
   97    MMMDC
-  90
-----
    7    MMMDCXC
-   5   (skipped several numerals because they were too big)
-----
    2    MMMDCXCV
-   1
-----
    1    MMMDCXCVI
-   1
-----
    0    MMMDCXCVII

And now I am out of numerals to subtract with, I would stop and say MMMDCXCVII is my answer.
Notice how I looped across all 13 numerals (even though I "skipped" some, my brain still considered them for a moment). I also continued to subtract the same one until it was too big. Your program needs to have this same logic.

Now how would you loop across all 13 possible values?
Imagine you have an array of 10 numbers:
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Imagine looping across all 10 numbers and printing them:
1
2
3
4
for(int i = 0; i < 10; i++) {
    cout << array[i] << " ";
}
cout << endl;
Last edited on
I haven't learned any of that in my class, so I wouldn't no how to apply it to my program. My instructor told me that I can do a while loop or something, but I wasn't sure what he meant or how I can put it into my program.

1
2
3
4
5
6
7
8
9
10
11
12
13
}

string roman_from_int(short num) 
{
    while(?num?)
    string romnum = "";
    {
    if(num >= 1000)
    {
        num = num - 1000;
        romnum = romnum + "M";
    }   


Would I have to put do something like:
1
2
while (x > 1000)
~~~~~// make it output M 3 times 
You seem to not know how to write any loops, so I strongly suggest you work through the tutorial and find out what types of loops are available to you in c++:
http://www.cplusplus.com/doc/tutorial/control/
After you understand loops, I should be able to help you better or you may be able to understand better what I have already posted.
My instructor just looked at my code and started "fixing" it for me, and I wasn't too sure what he was doing... -_- He doesn't explain things either... I am very lost in the class. :(
Pages: 12