Hello.
I'm taking a C++ course were the explanations are bad and we program only 3 times a semester. Therefore, my knowledge of C++ programming comes from trial-and-error.
I'm sure that everyone had to do the Roman numerals to decimal conversion program at one point. The assignment goes something like this:
Write a program to convert numbers entered in Roman Numeral to decimal. Your program should consist of a class, say romanType. An object of the type romanType should do the following:
a. Store the number as a Roman numeral
b. Convert and store the number into decimal
I wrote a code that does the conversion, :) but I don't know how to separate the code into classes. :?: Please help out.
//This program converts Roman numerals into Arabic numerals.
//It works properly only for correctly written Roman numerals.
//It works only for letters that correspond to Roman numerals.
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char roman_num[10];
cout << "Enter a Roman Numeral: ";
cin >> roman_num;
int len = strlen(roman_num);
int number = 0;
int counter = 0;
int b4sum = 0;
int sum = 0;
for (counter = len; counter >= 0; counter--)
{
if (roman_num[counter] == 'M')
number = 1000;
else if (roman_num[counter] == 'D')
number = 500;
else if (roman_num[counter] == 'C')
number = 100;
else if (roman_num[counter] == 'L')
number = 50;
else if (roman_num[counter] == 'X')
number = 10;
else if (roman_num[counter] == 'V')
number = 5;
else if (roman_num[counter] == 'I')
number = 1;
else
number = 0;
if (b4sum > number)
sum = b4sum - number;
else
sum = sum + number;
b4sum = number;
}
cout << "The Arabic Numeral is: " << sum << endl;
return 0;
}
thanks a lot.