Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> using namespace std; int romanCharValue(ch

ID: 674899 • Letter: #

Question

#include <iostream>
#include <string>
using namespace std;

int romanCharValue(char r)
{
if(r == 'M')
return 1000;
else if(r == 'D')
return 500;
else if(r == 'C')
return 100;
else if(r == 'L')
return 50;
else if(r == 'X')
return 10;
else if(r == 'V')
return 5;
else if(r == 'I')
return 1;
return 0;   
}

int convertRomanToInt(string s)
{
int i, total = 0;
char c, cnext;
for (i=0; i<(s.length()-1); ++i)
{
c= s.at(i);
cnext = s.at(i + 1);
if(romanCharValue(c) >= romanCharValue(cnext))
{
total += romanCharValue(c);
}
else
{
total += (romanCharValue(cnext) - romanCharValue(c));
i++;
}   
}
total += romanCharValue(s.at(i));
return total;
}

  
int main()
{
string roman;
while(1)
{
cout<<"Enter Roman number or Q to quit: ";
cin>>roman;
if(roman.at(0) == 'Q')
exit(0);
cout<<roman<<" = "<<convertRomanToInt(roman)<<endl;
}
}

Enter Roman number or Q to quit: I              First Attempt Correct?         Y | N

I = 1

Enter Roman number or Q to quit: V              First Attempt Correct?         Y | N

V = 5

Enter Roman number or Q to quit: X              First Attempt Correct?         Y | N

X = 10

Enter Roman number or Q to quit: L              First Attempt Correct?         Y | N

L = 50

Enter Roman number or Q to quit: C              First Attempt Correct?         Y | N

C = 100

Enter Roman number or Q to quit: D              First Attempt Correct?         Y | N

D = 500

Enter Roman number or Q to quit: M              First Attempt Correct?         Y | N

M = 1000

Enter Roman number or Q to quit: IV                    First Attempt Correct?         Y | N

IV = 4

Enter Roman number or Q to quit: MCMLXII        First Attempt Correct?         Y | N

MCMLXII = 1962

Enter Roman number or Q to quit: MDCCCLXXXVIII First Attempt Correct?         Y | N

MDCCCLXXXVIII = 1888

Enter Roman number or Q to quit: Q

Explanation / Answer

The following program will convert the roman number into its corresponding natural number.