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.