0

Hello guys, I am writing a C++ program that converts arabic to roman numerals or vice versa up to 100 (C).
The program should:

Handle all values for 1 (Roman I) to 100 (Roman C)
Permit entry in either format Arabic (1, 2, 3, ..., 100) or Roman (I, II, III, ..., C)
Reject any entries that are not correct with an appropriate error message
Be well commented
Use meaningful variables

My problem is that when I enter an arabic number I cannot convert it into a roman numeral.
Thanks in advance!
This is the code I have so far:

// Example program
#include <iostream>
#include <string>
using namespace std;

void roman2arabic(string);
void arabic2roman(string);
int main()
{
    while(1){
        cout << "Enter a Roman or Arabic Number to be converted- Q to quit: ";
        string input;
        getline(cin, input);
        if (input == "Q" || input == "q") return 0;
        roman2arabic(input);
        arabic2roman(input);
    }
    return 0;
}

void roman2arabic(string num){
    int value = 0;              //this will be the arabic value
    char prior = 'n';           // use this for IV, IX, etc.
    for (int i=0; i<num.length(); i++){
        switch(num[i]){
            case 'I':
                value++;
                break;
            case 'V':
                if(prior == 'I') value += 3;
                else value += 5;
                break;
            case 'X':
                if(prior == 'I') value += 8;
                else value += 10;
                break;
            case 'L':
                if(prior == 'X') value += 30;
                else value += 50;
                break;
            case 'C':
                if(prior == 'X') value += 80;
                else value += 100;
                break;
        }
        prior = num[i];         // so that I can look back dor the I
    }
    cout << "The arabic value for " << num << " is " << value << endl;
}
void arabic2roman(string num){
    int value = 0;              //this will be the roman value
    char prior = 'n';           // use this for 4, 9, etc.
    for (int i=0; i<num.length(); i++){
        switch(num[i]){
            case '1':
                value++;
                break;
            case '5':
                if(prior == '1') value += 3;
                else value += 5;
                break;
            case '10':
                if(prior == '10') value += 8;
                else value += 10;
                break;
            case '50':
                if(prior == '10') value += 30;
                else value += 50;
                break;
            case '100':
                if(prior == '10') value += 80;
                else value += 100;
                break;
        }
        prior = num[i];         // so that I can look back dor the I
    }
    cout << "The roman value for " << num << " is " << value << endl;
}

Edited by happygeek: moved

3
Contributors
3
Replies
41
Views
2 Months
Discussion Span
Last Post by Jonathan_30
1

You should have some kind of dictionary or an array of structs or so, to do the translation.

0

Thanks both for the replies!
I'll look into the suggestions to see how I can improve upon them and update what I have.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.