I am supposed to be creating a program that converts roman numbers to its decimal values. I have done something, but the math is wrong. When entering MCMLXXVIII, I am supposed to get 1978, and instead I get 2178. I am using substrings to solve this problem, but I am not getting anywhere. Can somebody please advise me on what to do?
#include<iomanip>
#include<iostream>
#include<string>
usingnamespace std;
int main(){
int M = 1000;
int D = 500;
int C = 100;
int L = 50;
int X = 10;
int V = 5;
int I = 1;
int num = 0;
cout << " Enter the Roman Numeral Value: ";
string roman;
cin >> roman;
string sub = roman.substr(0, 2);
cout << sub << endl;
for (int i = 0; i < roman.length(); i++)
{
switch (roman.at(i))
{
case'M':
case'm':
num += M;
break;
case'D':
case'd':
num += D;
break;
case'C':
case'c':
num += C;
break;
case'L':
case'l':
num += L;
break;
case'X':
case'x':
num += X;
break;
case'V':
case'v':
num += V;
break;
case'I':
case'i':
num += I;
break;
}
}
cout << num << endl;
}
#include<iomanip>
#include<iostream>
#include<string>
usingnamespace std;
constint I = 1;
constint V = 5;
constint X = 10;
constint L = 50;
constint C = 100;
constint D = 500;
constint M = 1000;
constint bad_val = -1;
// This function Takes the number and changes it into
// a Roman Numeral.
char Romandigit ( int val )
{
char digit_char;
if ( val == I )
digit_char = 'I';
if ( val == V )
digit_char = 'V';
if ( val == X )
digit_char = 'X';
if ( val == L )
digit_char = 'L';
if ( val == C )
digit_char = 'C';
if ( val == D )
digit_char = 'D';
if ( val == M )
digit_char = 'M';
return digit_char;
}
//This function Takes the Roman Numeral and changes it into a
//number and it also tests to find if a bad value is
//present.
int Roman_digit_Value( char roman_digit )
{
int val;
if ( roman_digit == 'I' )
val = I;
elseif ( roman_digit == 'V' )
val = V;
elseif ( roman_digit == 'X' )
val = X;
elseif ( roman_digit == 'L' )
val = L;
elseif ( roman_digit == 'C' )
val = C;
elseif ( roman_digit == 'D' )
val = D;
elseif ( roman_digit == 'M' )
val = M;
else
val = bad_val;
return val;
}
//This function prints the roman numeral as it reads the smallest
//amount of Roman Numerals possible.
void Print_Roman_Numeral ( int val )
{
int number = val;
int high_digit = M;
while ( number > 0 )
{
if ( number > high_digit )
cout << Roman_digit_Value ( high_digit );
number = number - high_digit;
if ( number > V )
cout << Roman_digit_Value ( V );
number = number - V;
if ( number > X )
cout << Roman_digit_Value ( X );
number = number - X;
if ( number > L )
cout << Roman_digit_Value ( L );
number = number - L;
if ( number > C )
cout << Roman_digit_Value ( C );
number = number - C;
if ( number > D )
cout << Roman_digit_Value ( D );
number = number - D;
if ( number > M )
cout << Roman_digit_Value ( M );
number = number - M;
}
}
//This function reads the input from the input file
//and then changes it to values.
void Print_Result ( int Roman_Num, int Second_Roman_Num, int Result, char operation )
{
if ( operation == '+' )
Print_Roman_Numeral ( Result );
cout << "The sum of " << Roman_Num << " and " << Second_Roman_Num <<
" is " << "(" << Result << ")" << endl;
if ( operation == '-' )
cout << "The difference of " << Roman_Num << " and " << Second_Roman_Num <<
" is " << "(" << Result << ")";
if ( operation == '/' )
cout << "The quotient of " << Roman_Num << " and " << Second_Roman_Num <<
" is " << "(" << Result << ")";
if ( operation == '*' )
cout << "The quotient of " << Roman_Num << " and " << Second_Roman_Num <<
" is " << "(" << Result << ")";
}
//This function turns the two romman numeral numbers
//into one by doing the specified operation.
int Return_Operation ( int Roman_Num, int Second_Roman_Num, char operation )
{
int Result = 0;
if ( operation == '+' )
Result = Roman_Num + Second_Roman_Num;
if ( operation == '-' )
Result = Roman_Num - Second_Roman_Num;
if ( operation == '/' )
Result = Roman_Num / Second_Roman_Num;
if ( operation == '*' )
Result = Roman_Num * Second_Roman_Num;
return Result;
}
//This function takes the roman digits thet were inputted
//and then changes them to a number value and it
//determines weaher the value is bad.
int Get_Roman_Numeral()
{
int num = 0;
char ch = 0;
int count = 1;
while (count <= 7 )
{
cin >> ch;
int value = Roman_digit_Value(ch);
num = num + value;
count++;
}
return num;
}
int main()
{
char operation = '+';
string input_Roman;
int Roman_Num = 0;
int Second_Roman_Num = 0;
int Result = 0;
Roman_Num = Get_Roman_Numeral();
while ( !cin.eof() )
{
cout << "The first number is " << Roman_Num << endl;
Second_Roman_Num = Get_Roman_Numeral();
cout << "The second number is " << Second_Roman_Num << endl;
cin >> operation;
cout << "Arithmetic operation is " << operation << endl;
Result = Return_Operation (Second_Roman_Num, Roman_Num, operation);
Print_Result( Roman_Num, Second_Roman_Num, Result, operation );
Roman_Num = Get_Roman_Numeral();
}
return 0;
}
Your main problem in the Roman Numeral to decimal conversion program, is you're not checking if a number is to be subtracted from a larger number. If the value of a Roman Numeral is lower than the next value, that value gets subtracted. You are just checking the values, and adding.
Value .. MCMLXXVIII
M = 1000
C is less than the next M, so 100 is subtracted from the 1000 = 900
L = 50
X = 10
X = 10
V = 5
I = 1
I = 1
I = 1
Yeah I know, but I tried everything, and the program doesnt do what its supposed to. The value doesnt come out to 1978. I dont know how to check if the number is larger than the previous number using substrings and void function, as that is what I am supposed to do