# Roman Numeral...T_T

• 04-10-2007
only1st
Roman Numeral...T_T
i am very very very new to computer sicence thing...
converting number to roman numeral is kinda hard for me
i learned looping,branching,and strings
can u guys help me?

Code:

```#include<iostream> using namespace std; int main() {  short x;    cout << "Enter your number:"   <<endl;  cin >> x;    if(x == 1)  {   cout << "I";  }  else if(x == 5)  {   cout << "V";  }  else if(x == 10)  {   cout << "X";  }  else if(x == 50)  {   cout << "L";  }  else if(x == 100)  {   cout << "C";  }  else if(x ==500)  {   cout << "D";  }  else if(x == 1000)  {   cout << "M";  }  else  {   cout << "error\n"   <<endl;  }    return 0; }```
• 04-10-2007
what are you having problems with?
• 04-10-2007
only1st
converting 1 - 3999 but i dont know how to do it...
• 04-10-2007
laserlight
What is your algorithm, or at least what steps do you have in mind?
• 04-11-2007
Daved
Hint: Many numbers use multiple characters when converted to roman numerals. Your current code will only ever output a single character, so you need to run that code over and over until all the correct characters are output.
• 04-11-2007
only1st
Code:

```#include<iostream> #include<string> using namespace std; int main() {  short x;  string roman;    cout << "Enter your number:"<<endl;  cin >> x;  while (!(x < 4000 && x > 0))  // I should probably DeMorgan's this...  {   cerr << "\n\aInvalid range!  Your value must be between 1 "           "and 3999!\nSo sayeth the Emperor!\n\n";   cout << "Enter your number:"<<endl;   cin >> x;  }    // need to 'combine' things so that they can do non-exact multiples:  1776, 931, 452, 8, ...    if(x == 1000)  {   roman = "M";  }  else if(x == 500)  {   roman = "D";  }  else if(x == 100)  {   roman = "C";  }  else if(x == 50)  {   roman = "L";  }  else if(x == 10)  {   roman = "X";  }  else if(x == 5)  {   roman = "V";  }  else if(x == 1)  {   roman = "I";  }    // x/1000 = t;  // count << "M" * t <<endl;  // print out t amout of times        cout << x << " translated to Roman Numerals is " << roman << ".\n";  return 0; }```
i only learned branching,looping,and string, and few basic things

how should i combine the things and do the non-exact number?

thank you for helping me
• 04-11-2007
Daved
Can you write out in english how you would solve this problem. Convert 79 to roman numerals and write down each step. That will be the first part of writing the correct code.
• 04-11-2007
only1st
yes.. convert 79,1234,874 ... ne numbers in to roman numeral
• 04-11-2007
laserlight
You have already described the problem. What you should do now is come up with a solution.

For example, suppose I want to give converting 16 to Roman numerals as an example towards getting a general solution. I might think:
16 >= 10, so I need an X.
16 - 10 = 6.
Now, 6 >= 5, so I need a V.
6 - 5 = 1.
1 is just I.
Thus 16 is XVI.

So, how would you solve it if you are given 79? What about say, 2007?
• 04-11-2007
dwks
It's much like converting numbers to other bases, such as binary, if you've ever done that (well, it's similar to one way of doing it). Take out the largest number you can. Repeat until you have zero.
• 04-11-2007
only1st
oh ok ty guys^^ it was very helpful^^ ill try to do it now
• 04-11-2007