I have to create a program for converting Roman numerals to decimal numbers for which I'm getting garbage value as an output. The fact is I have double checked my logic and it seems to be correct.
How can I correct it?
Here's my code:
#include<iostream>
#include<cstring>
using namespace std;
class RomanType
{
char str[10];
int d;
public:
void accept()
{
cout<<"Enter Roman No. in capitals:"<<endl;
cin>>str;
convert(str);
}
void convert(char str1[10])
{
int j=0;
for(j=0;j<strlen(str1);j++)
{
if( str1[j]=='I')
{
if(str1[j+1]=='V' || str1[j+1]=='X')
{
d=d-1;
cout<<j<<endl;
}
else
{
d=d+1;
cout<<d<<endl;
}
}
if ( str1[j]=='V')
d=d+5;
if(str1[j]=='X')
{
if(str1[j+1]=='L' || str1[j+1]=='C')
d=d-10;
else
d=d+10;
}
if(str1[j]=='L')
d=d+50;
if( str1[j]=='C')
{
if(str1[j+1]=='D' || str1[j+1]=='M')
d=d-100;
else
d=d+100;
}
if(str1[j]=='D')
d=d+500;
if(str1[j]=='M')
d=d+1000;
}
}
void display()
{
cout<<"It's decimal equivalent is="<<d<<endl;
}
};
main()
{
RomanType obj;
obj.accept();
obj.display();
}
OK guys thanks for the help. It's solved now. I had done a blunder and that was initialised d again in convert(), so that had made it as a local variable. See the comments: