Roman number converter

This is a discussion on Roman number converter within the C++ Programming forums, part of the General Programming Boards category; Does anyone know any good code for converting Arabic numbers to Roman numbers? I need c++ code that can be ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Roman number converter

    Does anyone know any good code for converting Arabic numbers to Roman numbers? I need c++ code that can be used on Windows C++ Visual Studio. Appreciate any help that I can get.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Go nuts with modulus. It will give you the remainders of each division, 1000, 100, 50, 5, whatever, and you can output the correct letter.. or set of letters.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    smells like homework to me.

    how about you try and do it yourself and post with specific questions.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Use switch statement as follows:

    Code:
    while (loopCount < firstRoman.length()) 
    		{ 
    			switch(firstRoman[loopCount++]) 
    			{ 
    				case 'I': 
    				first += 1; 
    				break; 
    			
    				case 'V': 
    				first += 5; 
    				break; 
    				
    				case 'X': 
    				first += 10; 
    				break;
    				
    				case 'L': 
    				first += 50; 
    				break;
    				
    				case 'C': 
    				first += 100; 
    				break;
    				
    				case 'D': 
    				first += 500; 
    				break;
    				
    				case 'M': 
    				first += 1000; 
    				break;
    
    				default: 
    				cout << endl << " You entered an invalid numeral. Goodbye." << endl << endl;
    				
    				return;

  5. #5
    Unregistered
    Guest
    the capital letters MDCLXVI are constants and these converts the integer to the Roman numeral (adding style) ie no IV for 4. and it is for straight roman numerals no extended.
    Code:
    string Convert(int digit)
    {
      string temp="\0";
    
      while (digit != 0)
      {
        if (digit > 999)
          {
            digit = digit - M;
            temp = temp + 'M';
          }
        else
          if (digit > 499)
            {
              digit = digit - D;
              temp = temp + 'D';
            }
          else
            if (digit >99)
              {
                digit = digit - C;
                temp = temp + 'C';
              }
            else
              if (digit > 49)
                {
                  digit = digit - L;
                  temp = temp + 'L';
                }
              else
                if (digit > 9)
                  {
                    digit = digit - X;
                    temp = temp + 'X';
                  }
                else
                  if (digit > 4)
                    {
                      digit = digit - V;
                      temp = temp + 'V';
                    }
                  else
                    {
                      digit = digit - I;
                      temp = temp + 'I';
                    }
      }//end while
      return (temp);
    }//end Convert ()

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    convert

    I know its ugly but....

    Code:
    #include <iostream.h>
    /*
    i 1
    v 5
    x 10
    l 50
    c 100
    d 500
    m 1000
    */
    int main()
    {
    	int equiv2[7];
    	char equiv1[7];
    
    	equiv2[0]=1;
    	equiv2[1]=5;
    	equiv2[2]=10;
    	equiv2[3]=50;
    	equiv2[4]=100;
    	equiv2[5]=500;
    	equiv2[6]=1000;
    
    	equiv1[0]='I';
    	equiv1[1]='V';
    	equiv1[2]='X';
    	equiv1[3]='L';
    	equiv1[4]='C';
    	equiv1[5]='D';
    	equiv1[6]='M';
    
    	char enter[20];
    	int tmp[20],
    		out=0,
    		z;
    	
    	for(int i=0; i<20; i++)
    		enter[i]=' ';
    	for(i=0; i<20; i++)
    		tmp[i]=0;
    
    	cout<<"enter the roman numeral:";
    	cin>>enter;
    	
    	i=0;
    	while(i<20&&enter[i]!=' ')
    	{
    		for(z=0; z<7; z++)
    		{
    			if(equiv1[z]==enter[i])
    			{
    				tmp[i]=equiv2[z];
    				break;
    			}
    		}
    		i++;
    	}
    	i=0;
    	z=0;
    	while(i<20&&enter[z]!=' ')
    	{
    		if(tmp[i+1]>tmp[i])
    			out+=tmp[i+1]-tmp[i];
    		else
    			out+=tmp[i+1]+tmp[i];
    	
    		i+=2;
    		z++;
    	}
    
    	cout<<endl<<out<<endl;
    
    	return 0; //notice the skillful use of int main, and how i return values ;)
    }
    Chance favors the prepared mind.

    Vis. C++ 6.0

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    this sounds like some one else in my class hmm... don't use arrays for they have not been taught in class yet *cough* You need at least 3 functions:

    1 - Convert from integer to Roman Numeral
    2 - Convert from Roman Numeral to integer
    3 - Check Roman Numeral for errors (arrangement and false letters)

    Of course they would be value returning like the string function I posted above.
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    3
    I am evidently not in your class, because we had arrays weeks ago and i can use them in this function. The array function code that you posted is for converting from Roman to Arabic, not from Arabic to Roman. I presume I can just change some of the variables around and it will work. Thanks for the help.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    I meant the string returning function up above the arrays that converts from an integer to a Roman Numeral. Not in my class , oh well. cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21