Roman Numbers

I have written the following code, however, I am having issues passing part 1.

#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;

std::string numerals = "VXLCDM";

int main()
{
char roman_Numeral;
int arabic_Numeral = 0;

while (cin.get(roman_Numeral))
{
if (roman_Numeral == 'M' || roman_Numeral == 'm')
arabic_Numeral = arabic_Numeral + 1000;

else if (roman_Numeral == 'D' || roman_Numeral == 'd')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral, 5) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 500;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 500;
continue;
}
}

else if (roman_Numeral == 'C' || roman_Numeral == 'c')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral, 4) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 100;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
continue;
}
}

else if (roman_Numeral == 'L' || roman_Numeral == 'l')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral, 3) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}

else if (roman_Numeral == 'X' || roman_Numeral == 'x')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}

else if (roman_Numeral == 'V' || roman_Numeral == 'v')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral, 1) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}

else if (roman_Numeral == 'I' || roman_Numeral == 'i')
{
roman_Numeral = cin.peek();
if (numerals.find(roman_Numeral) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 1;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 1;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}

The question sheet asks for:
Level 1: Units-only numbers
Begin by writing a program that converts a single Roman number in the range I (1) to IX (9) to Arabic form.
The program should read a single string from standard input and print the corresponding value on standard
output.
Hint: Use an array of strings to represent the Roman units digits, organised so that the string index
corresponds to the digit value. For example, the array would have the string “IV” at index position 4, and the
Flinders University / College of Science and Engineering 1
D or d:
L or l:
V or v:
five hundred
fifty
five
M or m:
C or c:
X or x:
I or i:
one thousand
one hundred
ten
one
Computer Programming 2
string “VII” at index position 7. Then convert the input string by searching through the array until you find a
match. The index where you find the match will be the digit value you need. Note that for reasons explained
below, it's easiest to begin at the "9" end of the array and work downwards.
Extend the program so that it works correctly when the input consists of either lower case or upper case
Roman letters. The simplest approach is to convert each character in the input word into uppercase before
trying to find a match. Run a loop over the characters in the string, using the index operator ([]) to access
each character and the toupper function (you’ll need to include the cctype header file) to get the uppercase
value.
Level 2: Multiple numbers
Extend the program so that it can deal with single-digit numbers of any value. A single-digit number is one
that consists only of thousands, hundreds, tens, or units. Thus LXX (70) and CD (400) are single-digit
numbers, but XIV (14) and MC (1100) are not. Use the same approach as for units digits, but with 4 different
arrays, one each for the thousands, hundreds, tens, and units digits. Try looking for thousands digits first,
then for hundreds, and so on. When you find a match in one of the arrays, print the corresponding value and
stop.
Extend the program so that it reads and converts all input numbers until end of file on standard input. You'll
probably be able to do this simply by adding an appropriate "reading loop" around the code that reads a
single line.
Level 3: Multi-digit numbers
Extend the program so that it can handle multi-digit numbers. The idea is to look for prefixes of the Roman
number that correspond to valid Roman digits, beginning with the thousands digits and working from the "9"
down to the "1" end. If you find a matching prefix, remove it and add the corresponding value to a
progressive total, then move on to the next digit position. Since each digit is unique (and since larger digits
are represented by longer strings than smaller ones), this process will always find the correct value. When
you’ve considered all possible digits, the progressive total is the answer you need.
Make sure the program behaves correctly even if the input does not consist of a valid Roman number. Simply
convert the longest valid prefix and ignore any remaining characters. If no prefix characters are valid, display
zero.
Level 4: Convert either way
Extend the program so that if the input is an Arabic number, the output is the corresponding Roman number.
Use the first character in the input word to decide which way to convert. If it's an Arabic digit, convert as
Flinders University / College of Science and Engineering 2
thousands hundreds tens units
1 M C X I
2 MM CC XX II
3 MMM CCC XXX III
4 CD XL IV
5 D L V
6 DC LX VI
7 DCC LXX VII
8 DCCC LXXX VIII
9 CM XC IX
Computer Programming 2
much as possible of the word to Roman. If it's a Roman digit letter, convert as much as possible to Arabic. If
it's neither, then output zero. Use the same digit arrays to convert both to and from Roman numbers.
Last edited on
> I've been asked to complete the following class questions.
And now you're asking us to complete the following class questions.

The deal is
- you attempt some part of the question.
- you get stuck
- you ask a specific question about your code and why you seem to be stuck

We answer your questions, and provide some guidance.

We are NOT your mules to carry you to the finishing line and your diploma.
Topic archived. No new replies allowed.