Roman Numeral...T_T

This is a discussion on Roman Numeral...T_T within the C++ Programming forums, part of the General Programming Boards category; i am very very very new to computer sicence thing... converting number to roman numeral is kinda hard for me ...

  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
    22,114
    What is your algorithm, or at least what steps do you have in mind?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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 12:42 PM. Reason: added more detials

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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
    22,114
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,048
    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, 07:23 PM
  4. Arabic to Roman Numeral
    By jdm71488 in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2004, 02:33 AM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21