This program challenge was given to me by my Professor for extra credit. He said it includes some programming that we haven't learned yet in our C++ class. Messed up...

Ideally I would love if someone would do this for me, but we all know that's not happening. lol

Anyways...could anybody offer an idea of what I would do/use?

"Write a program that will convert Roman numerals to Arabic numbers or Arabic numbers to Roman numerals.

Your program should prompt the user to select the type of conversion they would like to perform, Roman to Arabic or Arabic to Roman, and allow them to convert as many numbers as they would like."

Here's a quick refresher for roman numerals:

M = 1000
D = 500
C = 100
L = 50
X = 10
V = 5
I = 1

Thanks!

Recommended Answers

All 14 Replies

Have you written a change making program? It's the same concept for converting to roman numerals: spit out the largest possible denomination until there's nothing left in the remaining total. Start with that so you're no overwhelmed.

Do we get extra credit for helping you? :-)

Write down ALL of the rules to convert Roman to Decimal. Remember though L is 50 and X is 10, whether or not the 10 is added to or subtracted from the 5 depends upon what side of the L it is found. So, LX is 60 and XL is 40. Likewise for other numbers. V is 5, I is 1, VIII is 8, IV is 4, 3 is III, 2 is II, etc. So, what are ALL the rules? The reason why this is extra credit is that you need to use a read-ahead/backtrack algorithm to determine how to apply the math.

Ditto regarding rules to convert Decimal to Roman.

Thanks for the quick responses.

I really appreciate it.

@Narue: I've never written a change making program. I didn't find any reference of that in my textbook. What should I look under?

@rubberman: No EC for you, but you've my gratitude? lol The most useless and undesirable prize I'd say. lol sorry.

Thanks for the quick responses.

I really appreciate it.

@Narue: I've never written a change making program. I didn't find any reference of that in my textbook. What should I look under?

@rubberman: No EC for you, but you've my gratitude? lol The most useless and undesirable prize I'd say. lol sorry.

No problem. I like to get folks to think about their problems, as a first step toward solving them. Understanding the scope is essential in that. As a teacher, sometimes I have to get in my students' faces. We all like to take the easy way out, but that doesn't help us elevate our skills.

@Narue: I've never written a change making program. I didn't find any reference of that in my textbook. What should I look under?

It doesn't matter. If you've written a change program, you can use the concepts you learned. Since you haven't, you have to do this program without that background knowledge.

It's simply, if you enter 743, what's the largest Roman numeral that fits into this number. M doesn't. D does. Store the D and subtract the value leaving 243.

Now what's the largest? C, giving you DC and leaving 143. Continue until you have 0.

Once you get that done, add the functionality that instead of a simple CCCC you save the normal value CD instead. This is hard part.

Show us what you have so far, it'll give us an idea of how we can help you since this program takes a lot of work to make.

#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class RomanNumeral
{
public:
    const static int M = 1000;
    const static int D = 500;
    const static int C = 100;
    const static int L = 50;
    const static int X = 10;
    const static int V = 5;
    const static int I = 1;

    RomanNumeral( const int arabic ) :
    m_roman( "" ),
    m_arabic( ((arabic > 0) ? arabic : 0) ) 
    {
	if( arabic > 0 )
	{
	    int i = arabic;
	    while( i > 0 )
	    {
		if( ( i - M ) >= 0 )
		{
		    m_roman += "M";
		    i -= M;
		    continue;
		}
		if( ( i - D ) >= 0 )
		{
		    m_roman += "D";
		    i -= D;
		    continue;
		}
		if( ( i - C ) >= 0 )
		{
		    m_roman += "C";
		    i -= C;
		    continue;
		}
		if( ( i - L ) >= 0 )
		{
		    m_roman += "L";
		    i -= L;
		    continue;
		}
		if( ( i - X ) >= 0 )
		{
		    m_roman += "X";
		    i -= X;
		    continue;
		}
		if( ( i - V ) >= 0 )
		{
		    m_roman += "V";
		    i -= V;
		    continue;
		}
		if( ( i - I ) >= 0 )
		{
		    m_roman += "I";
		    i -= I;
		    continue;
		}
	    }
	}
	else
	{
	    m_roman = "0";
	}
    }

    RomanNumeral( const std::string& string ) :
    m_roman( ((string.size() > 0 ) ? string : "0" ) ),
    m_arabic( 0 )
    {
	int i = 0;
	while( i < (int)string.size() )
	{
	    char c = string[i++];
	    switch( c )
	    {
		case 'M':
		case 'm':
		    m_arabic += M;
		    break;
		case 'D':
		case 'd':
		    m_arabic += D;
		    break;
		case 'C':
		case 'c':
		    m_arabic += C;
		    break;
		case 'L':
		case 'l':
		    m_arabic += L;
		    break;
		case 'X':
		case 'x':
		    m_arabic += X;
		    break;
		case 'V':
		case 'v':
		    m_arabic += V;
		    break;
		case 'I':
		case 'i':
		    m_arabic += I;
		    break;
		default:
		    throw new std::out_of_range( "Not a valid Roman numeral!" );
		    break;
	    }
	}
    }	
	
    int getArabic()
    {
	return m_arabic;
    }
    void setArabic( const int arabic );
    const std::string& getRoman()
    {
	return m_roman;
    }
protected:
    std::string m_roman;
    int m_arabic;
};

int main()
{
    cout << "Welcome to the Roman numeral to Arabic numeral converter!\nPlease enter a number in Roman numerals to be converted: ";
    
    std::string roman;
    cin >> roman;
    try
    {
	RomanNumeral rn( roman );
	cout << "Roman Numeral: " << roman << " Arabic: " << rn.getArabic() << endl;
    }
    catch( exception* ex )
    {
	cout << roman << " " << ex->what() << endl;
    }

    cout << "Now enter a Arabic number to be converted to Roman numerals: ";
    int arabic;
    cin >> arabic;
    try
    {
	RomanNumeral rn( arabic );
	cout << "Arabic number: " << arabic << " Roman: " << rn.getRoman() << endl;
    }
    catch( ... )
    {
	cout << "error during processing...too bad, I'm outta here!" << endl;
    }

	system("pause");

    return 0;
}

I showed it to my professor.

He said all I need now is "if" statements, cause I told him my problem is when I put "9" in, I get "VIIII" instead of "IX".

So he said, place if-then statements saying no more than roman characters in a row and promote them.

He really lost me. >_<

I can see why.

He means that if you have VIIII, that's at the 5 level with more than 3 Is. You need to promote that group to the next level (5 -> 10) to IX.

IIII alone is the 1 level, promotes to V
VIIII is the 5 level, promotes to X
XIIII is back to the 1 level (X is not one step from I) so IIII promotes to V

Exact same pattern for XXXX and CCCC

See this. It's very enlightening.

what he's trying to say is, put smoe 'if' statements in there saying stuff like

if(i == 9){cout << "IX";}

and you have to do that for each numeral like that. The best way would probably be to put them in/around the first 'if' statements.

Make sense?

EDIT: I posted this before seeing there was a second page >.>

I need it in python program ...

I hope u can help me :)

Your needs are not our needs.
Show us what you have already in a NEW thread and maybe we'll help.
Try to read the rules of this site.

commented: He might read the rules if they weren't so bloody hard to find. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.