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;
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.

I can see how you might have one class and one 'driver' program, but I don't understand why you would want more than one class.

You could create, of course, a roman numeral class:

class Roman_Numeral
{
public:
Roman_Numeral(const char*);
~Roman_Numeral();

int get_conversion();
char get_numeral();

private: …
Answered by Bench 212 in a post from

I can see how you might have one class and one 'driver' program, but I don't understand why you would want more than one class.

My guess is that he wants one for roman numerals, one for Arabic numerals, etc. Maybe switching between the two using dynamic binding and inheritance.

All 5 Replies

I can see how you might have one class and one 'driver' program, but I don't understand why you would want more than one class.

You could create, of course, a roman numeral class:

class Roman_Numeral
{
public:
Roman_Numeral(const char*);
~Roman_Numeral();

int get_conversion();
char get_numeral();

private:
char numeral;
};

Something like that. It's early in the morning so there could be a few errors in there, but that should get you started. There are many ways of doing this, but that's just my \$.02.

I can see how you might have one class and one 'driver' program, but I don't understand why you would want more than one class.

My guess is that he wants one for roman numerals, one for Arabic numerals, etc. Maybe switching between the two using dynamic binding and inheritance.

[Edit: I just realised that by "arabic numerals" he meant "Decimal". However, the program could be converted to one of the greek numeration systems instead for example]

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;

To make life easier, you could replace all those cumbersome if/else statements with a std::map lookup table, (which could be incorporated to your Roman Numeral class) eg,

std::map<char, int> RomanDigits;
RomanDigits['I'] = 1;
RomanDigits['V'] = 5;
RomanDigits['X'] = 10;
// etc.

You would need an algorithm to handle the case where the input is not a valid roman numeral, for example, a find would return the end of the container if no match for the input is found.

Thanks for all your help, but think of me as the dumbest student ever because I cannot write a working class for my program. I know that I want one class 'romanType' that has three members. One gets the roman numerals, one converts it to a decimal number and the third one outputs the decimal. But I don't know how to write a class and implement it the right way so that the program works. Thanks.

>Thanks for all your help, but think of me as the dumbest student ever because I cannot write a working class for my program.

google + class design c++ ... god speed my friend :(

server_crash gave you a good outline for a class, including constructor & destructor.

If you've never created your own data types before, try a simple struct with some data members. In C++ both class and struct are essentially the same thing (the only difference is that class defaults members to private, and struct defaults members to public) You can then add some functions (eg, a "get data" or "set data" function).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.