Roman numbers to decimal numbers, getting garbage value

1.1k views Asked by At

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

There are 4 answers

0
Sahil Dhawan On BEST ANSWER

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

}
1
fjardon On

You didn't initialize d to 0. Please add this at the top of your convert function:

void convert(char str1[10])
{
    int j=0;
    d = 0;
    . . .
3
pm100 On

You didn't initialize d to 0

in convert put d=0 at the start

1
Ajay On

Few points:

  • Don't directly jump to parsing high value romans. Start with I, V, and X only (i.e. target 1 to 10 first, then 11 to 20, then 21 to 39, 40 to 99, 100 to 499 etc.)
  • Don't assume that if I is given, it is given before or after V or X. It may be given for itself (eg. II - you else part assumes something).
  • Assign value of d with zero
  • Do step debugging, watch value of d and other variables. If debugger isn't good or available, do output the values on each step/iteration.
  • [Add] You need not to pass str to function convert, since they belong to same class, and convert can/would read the same content.