Running Into Wall With Roman Numeral Calculator

Hello, can anybody help me conceptualize how to best move forward with this problem? I have a linked text file with lines like this

MCCXXVI CV +

I need my code to parse the lines, so it reads "M", it knows M = 1000, and then it adds up (M+C+C+X+X+V+I) + (C+V)and outputs the total. It should do that with every line and be able to parse all the arithmetic operators from the .txt file and apply them to the chars.

It was easy enough to add parse the chars and convert them to values. I'm lost now on the next steps, particularly adding up all these together. Is there another technique I can use to make this easier?

I spent a lot of time reading and watching videos only to still feel lost with this so please know I'm only asking because I've tried hard and run into a wall. My professor is already helping me out because this is the EASY version of the assignment.

After I make this all work in main, I'm to convert it to four functions. But for now, this is my quest.

Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  int main() {
    char romanNumeral; // declare romanNumeral char
    int romanValue{0};
    string romanString;


    cout << "Roman Numeral Calculator by Valon Jakupi\n";
    ifstream ltrData("/Users/victorstone/Desktop/WCC C++ Projects/mp4/mp4romanletrdata.txt");

    if (!ltrData){
        cout << "Unable to find the file\n";
    }
    while (!ltrData.eof()){       //While it's not at the end, it will read each character and place it in romanNumeral
        ltrData >> romanNumeral;




    if (ltrData.is_open()){  //While the program is open
     if (romanNumeral == 'I'){
         romanValue = 1;
    }
     if (romanNumeral == 'V'){
         romanValue = 5;
     }
      if (romanNumeral == 'X'){
          romanValue = 10;
      }
      if (romanNumeral == 'L'){
          romanValue = 50;
      }
       if (romanNumeral == 'C'){
           romanValue = 100;
       }
        if (romanNumeral == 'D'){
            romanValue = 500;
        }
        if (romanNumeral == 'M'){
            romanValue = 1000;
        }
    }

    cout << romanNumeral << "\n"; //Output romanNumerals and then new ine
    cout << romanValue << "\n"; // Output romanValues and then new line.

    }


    return 0;
}
It is more complicated than just adding, you need to subtract preceeding eg IV is V-I not V+I

you need to list out the rules for RNs.
from what I recall, if the left digit is < the right digit, you subtract it, and I think only a few values can be used for the left digit in that case.

you need to parse it looking at the left most digit first, and the look at the next digit too, decide if you add or subtract, then just total it up...

so, what I would do from what you have is read the RN as a string, not one character at a time.
then loop over the string, you now know how long it is (.length()) and can see if you need to check the right of the current for + or -, then add it up that way.

there are ways to make the letter/number conversion nicer, but what you have is fine for now.
eg
int convert[256]{0};

convert ['X'] = convert['x'] = 10; //fill in upper and lower case while you are at it
convert['I'] = 1;
... fill them all in, there are only like 10 values, 20 for the 2 cases...

Last edited on
Yes, good call, so for this exercise it is purely additive so there's one mercy.
You are to design and implement a Roman numeral calculator. The subtractive Roman numeral notation commonly in use today was 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 equals IIII in this notation, not IV). Each number starts with the digit of highest value and ends with the digit of smallest value. This is the notation you will use in this program.
Your program inputs two Roman numbers and an arithmetic operator and prints out the result of the operation, also as a Roman number. 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 number MDCCCCLXXXXVI represents 1996, because 1996 really consists of:
1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1.
M D C C C C L X X X X V I
The arithmetic operators that your program should recognize in the input are +, -, *, and /. These should perform the C++ operations of integer addition, subtraction, multiplication, and division.
One way of approaching this problem is to convert the Roman numbers into integers, then perform the required operation, and then convert the result back into a Roman number for printing.
Assume that the input numbers are in purely additive form - that is, digits are followed only by digits of the same or lower value. Also assume that the letters are all legal, no need to check for errors in the input file. Also, assume that the answer to each calculation will be a positive number.



If I render it as a string, I understand how that gives you more freedom, but how then could I add them up? How do you store a string while adding each character individually?
string rn{};
cin >> rn;
for(int i = 0; i < rn.length(); i++)
{
total+= letter_to_value(rn[i]);//whatever you need to do here to covert it.
//possibly complain if letter is not valid, eg Z or something.
}

and later that becomes something like

for(int i = 0; i < rn.length(); i++)
{
if( letter_to_value(rn[i]) < letter_to_value(rn[i+1])
total -= letter_to_value(rn[i])
else
total+= letter_to_value(rn[i]);//whatever you need to do here to covert it.
//possibly complain if letter is not valid, eg Z or something.
///possibly detect invalid subtractors
///possibly detect double subtractions (should only be 1 per group, you cant subtract twice in a row)
}

or in other words, the string stores the letters. You can then process the letters one by one, but now you can look ahead for that subtraction rule.... Try it with just the addition as required, but code it this way in prep for next week when you need to do the rest of the problem (maybe?).
Last edited on
Topic archived. No new replies allowed.