GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Apr-2008, 20:05
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Roman into Decimal Program


Hello everyone!

I wrote this program which is meant to convert Roman numerals into decimal. I checked a few Roman numerals on it, it does convert CCCLIX to 359 alright but not MCXIV to 1114 (instead the output is 1113) or MDCLXVI to 1666 (the output being 1663). I know there is some logic problem but I can't figure out what. Could anyone please have a look at it?

CPP / C++ / C Code:
#include<iostream>

using namespace std;

class RomanDecimal
{
public:
 void store ();
 void ConvertStore ();
 void print();
 RomanDecimal ();

private:
 char number[8];

 int counter;
};

int main()
{ 
 
 RomanDecimal convert;
 
 cout<<"This is a program to convert Roman numerals into Decimal"<<endl<<endl;

 convert.store ();
 convert.ConvertStore();
 convert.print ();

 return 0;

 

}


void RomanDecimal::store ()
{
 cout<<"Enter in Roman (capital letters)"<<endl;
 cin.getline(number, 8);


}

void RomanDecimal::ConvertStore()
{
	int count=0;
	for (int i=0;i<8;i++)
	{
	  switch(number[i])
  {
  case 'M':count=count+1000;
	  break;
  case 'D':count=count+500;
	  break;
  case 'C':count=count+100;
	  break;
  case 'L':count=count+50;
	  break;
  case 'X':

	  if (number[i-1]='I')

	  count=count+9;
	  else if (number[i+1]='I')
		  count=count+11;

	  break;

  case 'V':

	  if (number[i-1]='I')
		  count=count+4;
	  else if (number[i+1]='I')
		  count=count+6;

	  break;

  case 'I':

	  if ( (number[i-1]='X') || (number[i-1]='V') || (number[i+1]='X') || (number[i+1]='V') )
		  count=count;
	  else
		  count=count+1;


   break;
  };
	}


	counter=count;
}



void RomanDecimal::print()
{
 int c;
 c=counter;

 cout<<"Decimal="<<c<<endl;
}

RomanDecimal::RomanDecimal()
{
	for (int i=0;i<8;i++)
	{
		number[i]='0';

	}

	
	counter=0;
}
  #2  
Old 03-Apr-2008, 03:01
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 53
mamntc02 has a spectacular aura about

Re: Roman into Decimal Program


Hi,

You have some programming errors:
  • There's an out of bound error: Imagine you want to get decimal value of 'X', so number="X". When you treat with 'X' in your switch:
    CPP / C++ / C Code:
    case 'X':
    	if (number[i-1]='I')
    		count=count+9;
    	else if (number[i+1]='I')
    		count=count+11;
    	break;
    You are trying to access to number[-1]!!! Out of bounds. You'll have the same problem if length of number is 7 (it can't be 8, because of '\0' character), you'll try to access to number[i+1] = number[8], which is also an error.
  • Your computation problems are in 'X' and 'V' and 'I'. You always suppose 'X' and 'V' are next to an 'I' character, and that's no always true.
  • Last error is syntactic: To compare in C++ you must use the operator '==' not '=', which is only to assign.
Just check to convert 'X' or 'V', they give wrong values.

Regards
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #3  
Old 03-Apr-2008, 09:29
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Roman into Decimal Program


Thanks mamntc!

I have corrected the syntactic errors now, and edited the code to this for X
CPP / C++ / C Code:
case 'X':

      
	  if (number[i-1]=='I')

	  count=count+9;
      
	  else if ( (number[i+1]=='I') && (number[i+2]!='V') )
	  count=count+11;

	  else 
		  
		  count=count+10;
	  

	  break;

and this for V
CPP / C++ / C Code:
case 'V':


	  if ( (number[i-1]=='I') && (number[i-2]=='X') )
		  count=count+4;
	  else if (number[i+1]=='I')
		  count=count+6;
	  else 
		  count=count+5;
	  
	   break;

It gives the numbers correctly, at least the ones I checked. Is it okay now?

How do I correct the out-of-bounds error?
  #4  
Old 03-Apr-2008, 10:06
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 53
mamntc02 has a spectacular aura about

Re: Roman into Decimal Program


Quote:
Originally Posted by hira
How do I correct the out-of-bounds error?
The easiest way is checking each possible out-of-bounds:
CPP / C++ / C Code:
if (i>0){
	if (number[i-1]=='I')
		count+=9;
}
else if (i < strlen(name)) && {number[i-1]) == 'I')
	count+=11;
	
}
else count+= 10;
Another way could be, overloading [] operator, for example:
CPP / C++ / C Code:
char& operator[](const int i ){
	if (i < 0) return -1; // or thrown an exception
	if (i > strlen(this.name)) return -1;
	
	// rest of code
}
The last possibility is, rather than use a char[8] variable, use a string instance, which is safer using at() member function.

Regards.
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #5  
Old 03-Apr-2008, 10:24
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Roman into Decimal Program


Thanks once again!

We haven't done operator overloading so I am gonna skip that at the moment.

As for the other code, I am going to try implementing that, though I was wondering if it does calculate okay without (if>0) condition, whats the point of it? The code for number[-1] won't implement in either case (correct me if I am wrong, my concepts aren't too good)
  #6  
Old 04-May-2008, 00:27
zatora zatora is offline
New Member
 
Join Date: May 2008
Posts: 25
zatora is on a distinguished road

Re: Roman into Decimal Program


i have the same problem in my class i am wondering if u have the full solution to the roman--> decimal program, although i get the coding part for the function i am not good at class coding so here is the shortest way to have a conversion function i am assuming that u used a select case for the M=1000,V=5.....

CPP / C++ / C Code:
void convert_roman(int& len,char[] roman,int val[] )
{
// select case codeing part
int i;
for (i=len;i>=1;i--)
    {
        if (roman[len-1]>=roman[len])
            sum = val[len]+val[len-1];
        else
            sum=val[len]-val[len-1];
    }
}
Last edited by admin II : 04-May-2008 at 17:19. Reason: Please surround your C++ code with [cpp] your code [/cpp]
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11
help writing a roman numeral to decimal program Ichigo C++ Forum 3 23-Mar-2006 14:05
Frustration...Roman Numeral Program SpyD3r C++ Forum 14 13-Nov-2005 20:02
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 19:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.