Roman Numeral Converter

Alright, so I'm working on a program that is supposed to convert an input of Arabic Numeral 1-3999 into Roman Numerals.

I have my logic planned out, but I'm having horrible trouble with infinite loops. I want my program to begin by subtracting the input by one thousand. If the answer is greater than zero, that means that the output needs to be one "M." After that, it needs to keep subtracting 1000 until the result is less than zero.

The loop I've tried to make will just list off a bunch of M's. I think it keeps re-doing the initial subtracting and not looping back. (in other words, if the input is 2893, it will repeatedly subtract 1000 from 2893 instead of moving on to 1893-1000.

Any suggestions? What am I doing wrong?

Here's my code so far:

#include <iostream>;
using namespace std;
int main ()
{
int arabicNumeral =0;
int M =0;
int D =0;
int C =0;
int L =0;
int X=0;
int V=0;
int I=0;

cout << "Please enter a number to be converted into a roman numeral: " << endl;
cin >> arabicNumeral;

M=arabicNumeral-1000;

do {(arabicNumeral-1000);
cout << "M" << endl;}
while (M>=0);

return 0;
}

THANK YOU
The problem is with that loop.

The condition of the loop (M>=0) is independent of intructions from the loop. In other words, you have to put an instruction which is dependent of M (for instance: M=M-1000) in that loop so that the loop could be finished.

I think you code might be:

1
2
3
do{ M=arabicNumeral-1000;
                cout<<" M ";
                }while(M>=0);

Thank you so much for your help!

I tried this code and it still gave me a ridiculous amount of "M"s.

Hm.

Is there a way to break your loop, do you know?

Thanks again.
Your approach is unnecessarily complicated. There's no reason to use do .. while loop or subtracting 1000s all over again.

I'd recommend such a solution:

for (int M = arabicNumeral/1000; M>0; M--) cout << "M";
Last night I was a bit tired and I didn't give you a (very) good advice. Today I reviewed your code and I spoted a mistake and one note: You've put ; after header (#include<iostream> ). Even if the compiler don't see an error is better to not put ; after any header.

On the other hand, I modified a bit your program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main ()
{
int arabicNumeral=0,M=0,D=0, C=0, L=0, X=0, V=0,I=0;
cout << "Please enter a number to be converted into a roman numeral: " << endl;
cin >> arabicNumeral;

M=arabicNumeral-1000;
cout<<"M ";
while((M>1000)||(M==1000))
{   M=M-1000;
    cout<<" M ";
}

return 0;
}


Analyze a little:

The declaration of the variables is just on 1 line. I think is more easier that way (you don't ocupe thousands of lines).

On the other hand, that loop is transformed in other loop with initial test (while).

First of all, I initialize the variable M M=arabicNumeral-1000; and then, I print the " M " character because I got 1000 from variable arabicNumeral.
cout<<"M "; That is out of the loop (before the loop).

The loop condition is ((M>1000)||(M==1000)) because the variable M after she was initialized, could be 1000 or greater. In the loop, M decreases with 1000 and after that the program prints the " M " character. The loop's condition has the 1000 value as a comparation value because after the program decreases the M variable with 1000, if the M variable is <1000 the program will exit from the loop. If the comparation value in condition of that loop was 0, the program would printed the " M " character either the M was less than 1000.
Example of this note:

arabicNumeral = 2340.
M=1340 print " M "
enter in loop.
M=340 print " M "
if the condition was M>0, the program will print " M " (that means I've entered a number greater than 3 thousand which is incorrect)
but the condition is ((M>1000)||(M==1000)) so the program will exit from that loop. On the screen remain printed "M M" which is correct because the number 2340 is greater than 2000 (M M).

This is an understadable solution. Also, you can choose the Danielsson's solution which is correct too.
Last edited on
Thank you so much!!!

This worked great! (And it actually makes sense!)

Except the program seems to automattically assign a letter by default. I'm not sure how to explain it, but for example:

2511=MMDCLXVI

And it should be MMDXI, since roman numerals involve placing a character of lesser value to signify subtraction. (D=500 and C=100 so 400=CD)

Any suggestions?

One thing I tried was to delete the output cout<<""M"; that was located before each while loop, but then my program simply didn't work at all...

Thanks so much for your help.
Well, I will choose another solution more...beautiful.

For instance, you enter the number 2511. The prorgram should print MMDXI.
First of all you have to find the number of the figures of that number.
For example, the number 2511 has 4 figures.
This thing you could do with:

1
2
3
4
5
6
figures=0;
while(number)
{    number=number/10;
      figures++;
}
cout<<figures;

Then you have to generate a number which is multiple of 10. For example, for the number 2511 you have to generate 1000 and then you divide the number through 1000 (2511/1000%100). Now you have the thousands number (2). This number tells you how more " M " the program should print. Then you divide that number (1000) through 10. Now is 100. Then you divide the original number 2511 through that number (100). That means 2511/100%10. Now you got the hundred's number which is 5. That number tells you the number D or if is 3, it tells you the number of hundred (XXX, is't right?). And so on. The point is: you generate a number which is multiple of 10 ( after the formula: 10^(number_figures-1)) and you make the operation number/10_multiple%10multiple-1. You got that numbers and you have just to print the respective character. This is an easier version of your program (not implemented). I let you the implementation. If you don't do it after 5 days I will do it for you. But I want to see that you've tried.
Topic archived. No new replies allowed.