C++ Roman Numeral Calculator

Need some help here. Trying to create a calculator that does the math when inputtng 2 roman numerals and an operator. Below is my code it complies fine but wont let me input a second number or operator.

#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;


const int I = 1;
const int V = 5;
const int X = 10;
const int L = 50;

const int C = 100;

const int D = 500;
const int M = 1000;

const int 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;

else if (roman_digit == 'V')

val = V;

else if (roman_digit == 'X')

val = X;

else if (roman_digit == 'L')

val = L;

else if (roman_digit == 'C')

val = C;

else if (roman_digit == 'D')

val = D;

else if (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 main()
{

char operation;

string input_Roman;

int Roman_Num;

int Second_Roman_Num;
int Result;

cout << "Enter a Roman Numeral: " << endl;
cin >> Roman_Num;

cout << "Enter a second Roman Numeral" << endl;
cin >> Second_Roman_Num;

cout << "Please enter an Opertaion: " << endl;
cin >> operation;

void Print_result(int, int, int, int);
void Print_Roman_Numeral(int val);
int Roman_digit_Value(char roman_digit);
int Return_Operation(int Roman_Num, int Second_Roman_Num, char operation);

return 0;

}



Please use code tags. Edit your post, highlight the code and click the <> button to the right of the edit window. This lets us refer to line numbers in your code.

In your main() function, these lines are wrong:
1
2
3
4
void Print_result(int, int, int, int);
void Print_Roman_Numeral(int val);
int Roman_digit_Value(char roman_digit);
int Return_Operation(int Roman_Num, int Second_Roman_Num, char operation);

Those lines simply declare the functions. They don't call them. To call a function, you write it's name and put parentheses around the parameters. For example:
int val = Roman_digit_Value(ch);

Other comments:
Indent your code. This will help locate problems. For example, Print_Result() won't work as written.

I don't understand what Print_Roman_Numeral is supposed to do or how it works. The code looks like it's trying to convert a roman numeral to an integer, but the argument is an integer to begin with. If you start with an integer then there's no conversion to do.

You should probably represent roman numbers as strings. Then you need a function to convert the roman numeral string to an integer, and another function to convert an integer to a roman numeral string. You already have functions to convert individual roman characters, but not a whole number like MCMLXIII (1963).

The tricky part about handling roman numerals is that sometimes the character adds its value and sometimes it subtracts. For In the example above, C means subtract 100, not add 100.
If its worth doing, its worth 'over-doing'.
My Roman Numeral calculator I made from scratch:
http://pastebin.com/QiAAu7KW

Features:
-Binary operations such as | and & and ^
-complicated operators such as *=>
-and ignores non-roman-numeral characters (such as whitespace characters)
-clearly shows how Roman Numerals are impractical because (for example), if you put in MMMM**MMMM (which equals (MMMM pow 2)*MMMM), it out-puts a lot of M's. :)

NOTE: to run the code, you may need to update your boost libraries.
Last edited on
Topic archived. No new replies allowed.