Thread: Roman Numeral...T_T

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what are you having problems with?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    converting 1 - 3999 but i dont know how to do it...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your algorithm, or at least what steps do you have in mind?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    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
    Last edited by only1st; 04-11-2007 at 11:42 AM. Reason: added more detials

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    yes.. convert 79,1234,874 ... ne numbers in to roman numeral

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    oh ok ty guys^^ it was very helpful^^ ill try to do it now

  12. #12
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by dwks View Post
    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.
    Close but not quite. You have to consider numbers like 4. The roman numberal IV contains V which is larger than 4.
    Don't quote me on that... ...seriously

  13. #13
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    It would certainly be an interesting algorithm to write, have fun doing it!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  2. roman numeral conversion
    By clarky101 in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 07:13 PM
  3. Roman Numeral Calculator
    By The Brain in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 06:23 PM
  4. Arabic to Roman Numeral
    By jdm71488 in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2004, 01:33 AM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM