Roman Numeral Converter - C++ Write a program to read in Old Roman numerals and
ID: 668249 • 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>
using namespace std;
int cal(char ch){
if (ch == 'I') return 1;
if (ch == 'V') return 5;
if (ch == 'X') return 10;
if (ch == 'L') return 50;
if (ch == 'C') return 100;
if (ch == 'D') return 500;
if (ch == 'M') return 1000;
}
int main() {
string s;
int i;
while(true){
cin >> s;
i = 0;
for (int j = 0; j < s.length(); j++){
i += cal(s[j]);
}
cout << s << " " << i << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.