Question: My function for the roman numeral converter doesn't seem to work, like when I put in MCMVI (1906) it came out as 73. I'm not
My function for the roman numeral converter doesn't seem to work, like when I put in MCMVI (1906) it came out as 73. I'm not looking for a new program or anything, I just need the corrections for this to work.
Any modifications appreciated!
#include
using namespace std;
class romanNumber
{
public:
void setRoman(string);
int romanToInteger();
void printNumber();
void printRoman();
private:
string romanNum;
int decimal = 0;
};
int main()
{
string numerals;
cout << "Enter Roman Numerals ONLY: ";
cin >> numerals;
romanNumber Rom;
Rom.romanToInteger();
cin.get();cin.get();
return 0;
}
int romanNumber::romanToInteger() //error function
{
int cur_int = 0;
int next_int = 0;
int total = 0;
char numerals[7] = {'M','D','C','L','X','V','I'};
int length[7] = {'1000','500','100','50','10','5','1'};
total = numerals[length[] - 1]; //error
for (int i = length[] - 1; i > 0; i--) //error
{
cur_int = numerals[i];
next_int = numerals[i - 1];
if(cur_int <= next_int)
{
decimal += next_int;
}
else
{
decimal -= next_int;
}
}
cout << "Equivalent decimal is: " << total << endl;
return decimal;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
