I am trying to figure out how to convert Roman numerals to integers. This is part of my code. When I suggest the user to enter M, it shows 1000, but when I offer the user to enter a Roman numeral, such as VM, it does not give me 995, but instead of 1005. This is because I say that my program does just that.
What I'm trying to understand is how I can look to the future and find out when it adds or subtracts Roman numerals.
How do I start doing this?
class Roman { public int inprogress = 0; public Roman(string roman) { char temp = 'Z'; int length; length = roman.Length; for (int i = 0; i < length; i++) { temp = roman[i]; if (temp == 'M') { inprogress = inprogress + 1000; } if (temp == 'D') { inprogress = inprogress + 500; } if (temp == 'C') { inprogress = inprogress + 100; } if (temp == 'L') { inprogress = inprogress + 50; } if (temp == 'X') { inprogress = inprogress + 10; } if (temp == 'V') { inprogress = inprogress + 5; } if (temp == 'I') { inprogress = inprogress + 1; } } } }
source share