Results 1 to 5 of 5

Thread: Roman numerals

Threaded View

  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.

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