10 Replies - 7352 Views - Last Post: 15 March 2009 - 12:58 PM Rate Topic: -----

#1 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Roman Numeral Converter

Posted 26 February 2009 - 11:43 AM

I'm trying to write a program that will take some roman numerals entered in by the user and then display the numeric values (see code below). I have run into a problem that I can't seem to fix though; when the user enters in a set of roman numerals (say MMIII which should equal 2003) only the first roman numeral gets converted (so one 1000 would display. Surely there is something I'm over looking. I appreciate any help.

#include <iostream>

using namespace std;

// romanType constructor
class romanType{
public:
	romanType(char&);
	int convert();
	void print();
	void get();

private:
	int M, D, C, L, X, V, I;
	char romanNumeral;
};

// Set up Roman Numerals
romanType::romanType(char &ch){
	M = 1000;
	D = 500;
	C = 100;
	L = 50;
	X = 10;
	V = 5;
	I = 1;
	cout << ch << endl;
	romanNumeral=ch;
}

// Convert and return value
int romanType::convert(){
	if (romanNumeral == 'M')
	{
		return 1000;
	}
	else if(romanNumeral == 'D')
	{
		return  500;
	}
	else if(romanNumeral == 'C')
	{
		return  100;
	}
	else if(romanNumeral == 'L')
	{
		return  50;
	}
	else if(romanNumeral == 'X')
	{
		return  10;
	}
	else if(romanNumeral == 'V')
	{
		return  5;
	}
	else if(romanNumeral == 'I')
	{
		return  1;
	}
	return 0;
}


void romanType::print(){
	cout << romanNumeral << endl;
}


void romanType::get(){
}



int main(){
	char romanNumeral;
	cout << "Please enter a number in Roman numerals to be converted: ";
	cin >> romanNumeral;
	romanType roman=romanType(romanNumeral);
	cout << roman.convert() << endl;;

	system("pause");
return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Roman Numeral Converter

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Roman Numeral Converter

Posted 26 February 2009 - 11:48 AM

your romanType object is only constructed with one char, so it can only process one letter. You would need to use a string to make roman types and calculate the value of the entire statement from a string of chars instead of just one. You need to change the instance variable to a string :)

Hope this helps!
and welcome to DIC!

This post has been edited by crazyjugglerdrummer: 26 February 2009 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 11:49 AM

Unless I'm mistaken: romanType roman=romanType(romanNumeral); causes it to return 1000
(thus "roman = 1000" not += 1000)

This post has been edited by Hyper: 26 February 2009 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 12:59 PM

I tried both methods you two suggested and it still doesn't seem to work. When I change
romanType roman=romanType(romanNumeral); 
to
romanType roman+=romanType(romanNumeral); 
I get a couple errors. Also, when I try to change every occurance of char to a string I get errors as well (I made sure to include the string header). Still confused on what to do.
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 01:04 PM

I'll see what I can do, minute.
Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 01:27 PM

OK! It works perfectly fine. Sorry for the short delay, I've never attempted to do anything like this before... It's kind of fun trying new stuff.

Works like a charm.. Enjoy! :)
Oh, and remember: study it, don't just copy and paste what I did...

#include <iostream>
using namespace std;

class cRomanNumeral {

    private:
        int Decimal;             /* The total sum of the conversion              */
        int argumentSize;        /* Number of Roman Numerals to be converted     */
        char *romanNumeral;      /* The individual Roman Numeral to be converted */

    public:
        cRomanNumeral(char []);  /* The constructor                                  */
        void Convert();          /* The converter                                    */
        void display();          /* This isn't required, but it's fun to put it here */
};

cRomanNumeral::cRomanNumeral(char Digit[]) {

    Decimal = 0;
    romanNumeral = Digit;
    argumentSize = sizeof(romanNumeral) / sizeof(romanNumeral[0]);

    /* Convert the Roman Numeral digit to a base-10 decimal */
    Convert();
}

void cRomanNumeral::Convert() {

    for (int x = 0; x < argumentSize; x++) {

        switch (romanNumeral[x]) {
            case 'M': case 'm': Decimal += 1000; break;
            case 'D': case 'd': Decimal += 500; break;
            case 'C': case 'c': Decimal += 100; break;
            case 'L': case 'l': Decimal += 50; break;
            case 'X': case 'x': Decimal += 10; break;
            case 'V': case 'v': Decimal += 5; break;
            case 'I': case 'i': Decimal += 1; break;
        }
    }

    return;
}

void cRomanNumeral::display() { printf("%d", Decimal); return; }

int main() {

    char romanNumeral[15] = {'\0'};

    cout << "Please enter a number in Roman numerals to be converted: ";
    cin >> romanNumeral;
    _flushall();

    cRomanNumeral Roman(romanNumeral);
    Roman.display();

    cin.get();
    return 0;
}


If you have ANY questions about what's what or how it works, ask. :)

EDIT:
Somethings you could is actually improve it so it has mathmatically proper conversions. IV = "6" with this class.

This post has been edited by Hyper: 26 February 2009 - 01:33 PM

Was This Post Helpful? 1
  • +
  • -

#8 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 08:18 PM

Wow, I must say, that is much more help than I ever expected to get. This was extremely helpful and I appreciate you taking time out of your day to do all of that. I'm looking over the code right now and it really is helpful. Thank you for that.
Was This Post Helpful? 0
  • +
  • -

#9 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Roman Numeral Converter

Posted 26 February 2009 - 08:51 PM

You're very welcome.

I appreciate you using the little green button, and I hope you stick around the site for more questions... 'cause making (fixing?) that Roman Numeral converter was fun! :D
Was This Post Helpful? 0
  • +
  • -

#10 Newfie Ken  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-February 09

Re: Roman Numeral Converter

Posted 14 March 2009 - 10:33 PM

I just found this thread linked from another thread. I went through it and decided to see how the code Hyper posted worked. When I tried it, it did convert from roman numerals but it just added them together. Ex: IV. Normaly and properly this would be 4 (1 before 5), but when entered in the converter it is 6 (1 plus 5), because it only adds I (1) and V (5).

So I don't know it that is how the program was intended to work, but if it was, what would be a good way to go about making one convert it properly?
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Roman Numeral Converter

Posted 14 March 2009 - 11:00 PM

a really good topic on this was way back here.

They cover several different methods.


oopse should have really read the topic before commenting... sorry its really late here.

This post has been edited by NickDMax: 14 March 2009 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Roman Numeral Converter

Posted 15 March 2009 - 12:58 PM

Newfie Ken, Yes, the proper math isn't on it this (I fixed it and never posted the newer version).

            case 'I': case 'i': Decimal += 1;
            if (romanNumeral[x + 1] == 'v' || romanNumeral[x + 1] == 'V') { Decimal -= 2; }
            if (romanNumeral[x + 1] == 'x' || romanNumeral[x + 1] == 'X') { Decimal -= 2; }


You would simply have to account for all letters (real easy) and check for improper "conversions" (eg: IIIIII = 7) but I didn't write this to spoon-feed an answer, more or less to correct and show a single method it can be done. If I fixed all bugs, how would he learn or even have fun fixing it himself? :)

EDIT: Here's a snippet post containing that version (still incomplete): http://www.dreaminco...snippet3208.htm

This post has been edited by Hyper: 15 March 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1