Results 1 to 5 of 5

Thread: Roman numerals

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Roman numerals

    I am trying to make a program to convert from Roman numerals to base 10 numbers. I have the basic code, but the part I can't figure out is making numbers smaller if there is a smaller number in front of a larger one (example: I = 1, V = 5; I can do this but not IV which is 4). How can I do this? Here's what I have so far:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string input;
        int total = 0;
    
        cout << "Enter the Roman numeral: ";
        getline(cin, input);
    
        for (int i = 0; i < input.length(); i++)
        {
            switch(input[i])
            {
            case 'M':
                total += 1000; break;
            case 'D':
                total += 500; break;
            case 'C':
                total += 100; break;
            case 'L':
                total += 50; break;
            case 'X':
                total += 10; break;
            case 'V':
                total += 5; break;
            case 'I':
                total += 1; break;
            }
        }
    
        cout << input << " equals " << total << endl;
    
        return 0;
    }
    Last edited by Wynd; Jan 24th, 2002 at 09:50 PM.
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Don't you mean convert roman numerals to base 10?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Yeah
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Nice idea. I'll do a class that does this and post it here.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Here it is. Do whatever you want with it.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width