Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When testing my code for this problem I enter MCXIV which should display 1114, b

ID: 3636418 • Letter: W

Question

When testing my code for this problem I enter MCXIV which should display 1114, but instead it shows 1116. I am at a loss as to how to get aroudn that so the correct answer is printed. PLEASE HELP! Code is listed below as well. What is it that I am missing???

Write a program that converts a number entered in Romannumerals to decimal. Your program should consist of aclass, say, romanType. An object of type romanType should do the following:

a. Store the number as a Romannumeral.

b. Convert and store the number into decimalform.

c. Print the number as a Roman numeral ordecimal number as requested by the user.

The decimal values of the Roman numerals are:

M 1000

D 500

C 100

L 50

X 10

V 5

I 1

d. Test your program using the followingRoman numerals: MCXIV, CCCLIX, MDCLXVI.


#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;


class RomanNumeral
{
public:
const static int M = 1000;
const static int D = 500;
const static int C = 100;
const static int L = 50;
const static int X = 10;
const static int V = 5;
const static int I = 1;

RomanNumeral( const int decimal ) :
m_roman( "" ),
m_decimal( ((decimal > 0) ? decimal : 0) )
{
if( decimal > 0 )
{
int i = decimal;
while( i > 0 )
{
if( ( i - M ) >= 0 )
{
m_roman += "M";
i -= M;
continue;
}
if( ( i - D ) >= 0 )
{
m_roman += "D";
i -= D;
continue;
}
if( ( i - C ) >= 0 )
{
m_roman += "C";
i -= C;
continue;
}
if( ( i - L ) >= 0 )
{
m_roman += "L";
i -= L;
continue;
}
if( ( i - X ) >= 0 )
{
m_roman += "X";
i -= X;
continue;
}
if( ( i - V ) >= 0 )
{
m_roman += "V";
i -= V;
continue;
}
if( ( i - I ) >= 0 )
{
m_roman += "I";
i -= I;
continue;
}
}
}
else
{
m_roman = "0";
}
}

RomanNumeral( const std::string& string ) :
m_roman( ((string.size() > 0 ) ? string : "0" ) ),
m_decimal( 0 )
{
int i = 0;
while( i < (int)string.size() )
{
char c = string[i++];
switch( c )
{
case 'M':
case 'm':
m_decimal += M;
break;
case 'D':
case 'd':
m_decimal += D;
break;
case 'C':
case 'c':
m_decimal += C;
break;
case 'L':
case 'l':
m_decimal += L;
break;
case 'X':
case 'x':
m_decimal += X;
break;
case 'V':
case 'v':
m_decimal += V;
break;
case 'I':
case 'i':
m_decimal += I;
break;
default:
throw new std::out_of_range( "Not a valid Roman numeral!" );
break;
}
}
}

int getDecimal()
{
return m_decimal;
}
void setDecimal( const int decimal );
const std::string& getRoman()
{
return m_roman;
}
protected:
std::string m_roman;
int m_decimal;
};

int main()
{
cout << "Welcome to the Roman numeral to decimal converter! Please enter a number in Roman numerals to be converted: ";

std::string roman;
cin >> roman;
try
{
RomanNumeral rn( roman );
cout << "Roman Numeral: " << roman << " Decimal: " << rn.getDecimal() << endl;
}
catch( exception* ex )
{
cout << roman << " " << ex->what() << endl;
}

cout << "Now enter a decimal number to be converted to Roman numerals: ";
int decimal;
cin >> decimal;
try
{
RomanNumeral rn( decimal );
cout << "Decimal number: " << decimal << " Roman: " << rn.getRoman() << endl;
}
catch( ... )
{
cout << "error during processing...too bad, I'm outta here!" << endl;
}

return 0;
}

Explanation / Answer

// RomanType Header FIle // romanType.h #include using namespace std; class romanType { public : romanType( string = "" ); void setRoman( string ); void convertToDecimal(); void printRoman(); void printDecimal(); private: string roman; int decimal; }; // end class definition ofromanType // ----------------------------------------------------- // implementationfile romanTypeImp.cpp #include #include "romanType.h" using namespace std; romanType::romanType( string myRoman ) { roman = myRoman; decimal = 0; } // end constructor romanType void romanType::setRoman( string myRoman ) { roman = myRoman; decimal = 0; } // end function setRoman void romanType::convertToDecimal() { char romans[7] = { 'M', 'D', 'C', 'L','X', 'V', 'I'}; int decimals[ 7 ] = { 1000, 500, 100,50, 10, 5, 1 }; int j, pos; size_t len = roman.length(); // process the numeral for ( unsigned int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote