Cash-back offer from 25th to 30th November 2022. Get flat 10% cash-back credited to your account for a minimum transaction of $50. Post Your Question Today!

Question DetailsNormal
$ 18.00

Write a C++ Roman numeral calculator | Complete Solution

Question posted by
Online Tutor Profile

Emphasis on:  functions, loops, reading from a text file

Write a C++ Roman numeral calculator.  The subtractive Roman numeral notation commonly in use today (such as IV for 4) was apparently used only rarely during the time of the Roman Republic and Empire.  For ease of calculation, the Romans most frequently used a purely additive notation in which a number was simply the sum of its digits (4 is IIII in this notation).  Each number starts with the digit of highest value and ends with the digit of lowest value. 


The values of the Roman digits are as follows:


 I          =          1

 V         =          5

 X         =          10

 L         =          50

 C         =          100

 D         =  500

 M         =          1000


Thus, the Roman number MDCCCCLXXXXVIIII represents 1999 and MMXV represents 2015. 


Your program should read from a file a series of Roman numeral arithmetic problems.  Each problem consists of a Roman number, an arithmetic operator, and another Roman number.  Convert each Roman number into its decimal equivalent, perform the indicated arithmetic with the decimal equivalents to get a decimal result.  Then convert the decimal result back to a Roman number.  The arithmetic operators that your program should recognize in the input are +, -, *, and /.  These should perform the C++ standard operations of addition, subtraction, multiplication, and integer division.


Input:  text file ROMAN.TXT contains an unknown number of lines (so read until end of file) with each line in this format (with each Roman number terminated with a dollar sign):


L$ + DCV$



All input numbers and results will be positive values.  You can assume that the Roman digits are valid and that a Roman number is correctly formed (no digits out of order, like  MCDIL ).  All arithmetic operators will be valid.



Your output (either to the screen or to a text file) should show a copy of the input problem, the integer equivalents of the Roman numbers, the integer result, and the Roman result, like this

(space in between output values to enhance readability – don’t squish everything together):


MCCXXVI + LXVIIII = 1226 + 69 = 1295 = MCCLXXXXV

L + DCV = 50 + 605 = 655 = DCLV





  • You must use at least 4 user-defined functions in addition to main.


  • Copy the main function from the back of this page and write the four functions that are used.  The function names are shown in bold print, and the comments following them explain what they should do.


  • If you can’t get the EOF loop to work, use a for loop instead.  There are 10 problems in the file.








#include <iostream>

#include <fstream>

using namespace std;


// your 4 function prototypes go here


ifstream inFile ("roman.txt");


int main()


    char opChar;

    int leftOperand;

    int rightOperand;

    int result;


    if (

                cout << "can't open input file" << endl;

             else cout << "input file is open" << endl;


    while ( inFile.peek() != EOF ) 


        leftOperand = GetRoman ();    // read the first Roman number


// The GetRoman function reads a roman character from a file, displays a copy of it, and

// calculates and returns the decimal equivalent of the input roman number.  It should

// call another to get the integer equivalent for any single character roman digit:


//      thisDigit = RomanToInt (digit);    or    sum = sum + RomanToInt (digit);


// The same function is called again to read the second roman number, display a copy of it,

// and calculate and return its decimal equivalent. 


        inFile >> opChar;                   // read the operator

        cout << " " << opChar << " ";

        rightOperand = GetRoman ();  // read the second Roman number

        cout << " = " << leftOperand << ' ' << opChar << ' ' << rightOperand;


        inFile.ignore(1, '\n');              // skip over the end-of-line marker

        // If you’re using a counted loop, you won’t need this.


        result = DoTheMath (leftOperand, opChar, rightOperand);

        cout << " = " << result << endl;


// DoTheMath compares the input operator character to see which arithmetic operation is

// called for,  does the calculation, and returns the result back to main.


        cout << " = " ;

        ConvertIntToRoman (result);


// The ConvertIntToRoman function takes the integer result, converts it back into roman

// numerals, and displays the resulting roman number.



    return 0;




Available Answer
$ 18.00

[Solved] Write a C++ Roman numeral calculator | Complete Solution

  • This solution is not purchased yet.
  • Submitted On 25 Apr, 2015 06:34:15
Answer posted by
Online Tutor Profile
#include #include using namespace std; ifstream inFile ("roman.txt"); int GetRoman(); int RomanToInt(char); void ...
Buy now to view the complete solution
Other Similar Questions
User Profile

Write a critical appraisal that demonstrates

Write a critical appraisal that demonstrates comprehension of two quantitative research studies. Use the "Research Critique Guidelines – Part II" document to organize your essay. Successful completion of this assignment req...
User Profile

Please Write a Communication Plan Outline, Submit an

Abstract Whirlpool brand represents the Kitchen Aid brand. In 1986, the company was purchased by the Whirlpool Corporation. For this company, the approach of sales has been publicizing with online presence, in department stor...
User Profile

Write a C++ Roman numeral calculator | Complete Solution

#include #include using namespace std; ifstream inFile ("roman.txt"); int GetRoman(); int RomanToInt(char); void ConvertIntToRoman(int); int DoTheMath(int,char,int); int RomanToInt(char roman){ switch(...

The benefits of buying study notes from CourseMerits

Assurance Of Timely Delivery
We value your patience, and to ensure you always receive your homework help within the promised time, our dedicated team of tutors begins their work as soon as the request arrives.
Best Price In The Market
All the services that are available on our page cost only a nominal amount of money. In fact, the prices are lower than the industry standards. You can always expect value for money from us.
Uninterrupted 24/7 Support
Our customer support wing remains online 24x7 to provide you seamless assistance. Also, when you post a query or a request here, you can expect an immediate response from our side.

$ 629.35