Search code examples
c++data-conversiongarbageroman-numerals

Roman numbers to decimal numbers, getting garbage value


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();
}

Solution

  • 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:

    #include<iostream>
    #include<cstring>
    using namespace std;
    
    class RomanType
    {
    char str[10];
    int d;
    public:
    void accept()  // UNNECESSARILY NOT PASSING ANY STRING
    {
        cout<<"Enter Roman No. in capitals:"<<endl;
        cin>>str;
        convert();
    }
    
    void convert()
    {
         d=0;// PREVIOUSLY WRIITEN int d=0; so that was the mistake. Yay! it's solved :D
    
    
        for(int j=0;j<10;j++)
        {
    
        if( str[j]=='I')
        {
            if(str[j+1]=='V' || str[j+1]=='X')
            {
                d=d-1;
               // cout<<d<<endl;
            }
            else
            {
                d=d+1;
                //cout<<d<<endl;
    
            }
    
        }
    
        else if ( str[j]=='V')
            d=d+5;
    
        else if(str[j]=='X')
        {
            if(str[j+1]=='L' || str[j+1]=='C')
                d=d-10;
            else
                d=d+10;
        }
    
        else if(str[j]=='L')
            d=d+50;
    
        else if( str[j]=='C')
        {
            if(str[j+1]=='D' || str[j+1]=='M')
                d=d-100;
            else
                d=d+100;
        }
    
        else if(str[j]=='D')
            d=d+500;
    
        else if(str[j]=='M')
            d=d+1000;
        }
    
    
    }
    
    void display()
    {
        cout<<"It's decimal equivalent is="<<d<<endl;
    }
    };
    
    main()
    {
    RomanType obj;
    obj.accept();
    obj.display();
    
    }