Hey guys, i was looking through the internet for ways on how to solve converting roman numerals into integers and i found this method which is confusing me as im not able to understand it.
using namespace std;
int main()
{
char rom;
int n = 0;
int tot = 0 ;
int i;
cout << "Enter the roman numerals to convert" << endl;
while (rom != ""){
cin.get(rom);
if(rom=='M' || rom=='m')
{
tot=tot+1000;
}
else if(rom=='D' || rom=='D')
{
tot=tot+500;
}
else if(rom=='C' || rom=='C')
{
tot=tot+100;
}
else if(rom=='L' || rom=='l')
{
tot=tot+50;
}
else if(rom=='X' || rom=='x')
{
tot=tot+10;
}
else if(rom=='V' || rom=='v')
{
tot=tot+5;
}
else if(rom=='I' || rom=='i')
{
tot=tot+1;
}
}
cout<<"The value in Roman numerals is "<<tot<<endl;
}
from what i read up , cin.get basically reads one character at a time. So for example, if i were i were to type in "DDX" , it will read D then D again and finally X.
What i am really confused is how do they do the calculation. if one character is run through the if statement at a time, there should be a calculation error as for example, if i were to input in "CD" which actually means 400, the program will read "C" first which is 100 , followed by "D" , which is 500. so when it adds up , it should be 600? but when i tried running it with this coding, it manages to calculate 400. Anybody can explain which part am i wrong? or help explain how this coding works?