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

Roman Numeral Converter - C++ Write a program to read in Old Roman numerals and

ID: 668295 • Letter: R

Question

Roman Numeral Converter - C++

Write a program to read in Old Roman numerals and print out their value (base 10 numbers). Old Roman numerals do not have a smaller value in front of a larger one.

Example input:             Result:
III                                  III = 3
V                                   V = 5
XVIII                               XVIII = 18
MCCXXVI                    MCCXXVI = 1226

I is 1
V is 5
X is 10
L is 50
C is 100
D is 500
M is 1000

Cannot use arrays or classes as we have not covered them yet. Must use a while loop, and a switch. Use the enter key as the escape from the loop. (Empty line enter key to escape). Display must show what was entered plus the results.

Explanation / Answer


#include<iostream.h>
#include<conio.h>
#include<string.h>

int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
}
}

int getdec(const string& input)
{
int sum=0; char prev='%';
int i=(input.length()-1
while(i>=0)
{
if(value(input[i])<sum && (input[i]!=prev))
{ sum -= value(input[i]);
prev = input[i];
}
else
{
sum += value(input[i]);
prev = input[i];
}
   i--;
}
return sum;
}

void main()
{
   int a;
   char s[20];

   cout<<"Enter Roman number";
   cin>>s;

   a= getdec(s);
   cout<<a;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote