Roman Numeral Calculator

Hi, I'm working on a machine problem where I am to design a Roman Numeral calculator that inputs two Roman numbers and an arithmetic operator and prints out the result of the operation, also as a Roman number. I'm to use functions for the whole program and I can't reference global variables directly, I should be using parameter lists.

In order to get started with the rest of the functions I need to figure out how to read the input file. I'm trying to write the function get_Data.

"get_Data: This function receives the input file, reads one series of chars representing a Roman numeral, and sends back the value of the numeral read in. This function can call the function convert_from_Roman_to_Decimal to do the conversion while it is reading each letter."

The input file "mp4romanletrdata.txt" looks like this.
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *

I have a function that reads the file, but I can't figure out how to read the data individually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <fstream>

using namespace std;

ifstream inFile("mp4romanletrdata.txt");

int get_Data(ifstream&, char x)
{
	if (inFile.is_open())
	{
		while (inFile >> x)
		{
			cout << x;
		}
	}
	return x;
}
int main()
{
	get_Data(inFile, 1);
}
I'd do it more like this:
1
2
3
4
5
    string roman1, roman2;
    char oper;
    while (cin >> roman1 >> roman2 >> oper) {
        ...
    }


Then write the functions
unsigned romanToUnsigned(string &str);
string unsignedToRoman(unsigned num);
unsigned doOper(unsigned num1, num2, char oper);

and the loop becomes:
1
2
3
4
5
6
7
8
9
    string roman1, roman2;
    char oper;
    while (cin >> roman1 >> roman2 >> oper) {
        unsigned num1 = romanToUnsigned(roman1);
        unsigned num2 = romanToUnsigned(roman2);
        unsigned result = doOper(num1, num2, oper);
        string romanResult = unsignedToRoman(result);
        cout << roman1 << oper << roman2 << '=' << romanResult << '\n';
    }

When you write doOper(), add some temporary code to print the arguments and the answer. This will help a lot with debugging the code.
line 6: inFile is global. It needs to be inside main().

Lines 8-18: That function is going to read the entire file (echo it to cout) and return only the last character.

Line 8: Why is x passed as an argument?

Consider:
1
2
3
4
5
6
 
  string rn1, rn2;
  char op;
  
  while (inFile >> rn1 >> rn2 >> op)
    calculate (rn1, rn2, op);



Topic archived. No new replies allowed.