Hi,

I have been working on this program for a few days.. and I am so far gone I lost myself in my own program. I usually just start from scratch when I get frustrated but there is too much time invested into my heap to give up so I thought I would come here. My assignment is as follows:

Write a program that converts a number entered in Roman Numerals to decimal. Needs to consist of a class called romanType and an object that does the following:

Store the number as a Roman numeral
Convert and store the number into decimal form
Print the number as a Roman numeral or decimal number as requested by user

The decimal values of the roman numerals are:
M = 1000
D = 500
C = 100
L = 50
X = 10
V = 5
I = 1

After completion, I need to test the program with the following input: MCXIV, CCCLIX, MDCLXVI

Here's what I have so far in the source file:

//romanType.cpp

#include <iostream>
#include <iomanip>
#include "romanType.h"

using namespace std;

int main()
{
    romanType r;
    
    char romanNum;
    char choice;
    int decimal;
    int total;
    
    cout << setw(60) << "Welcome! Please follow the directions below." << endl;
    cout << endl;
    cout << "Please enter a Roman Numeral: ";
    cin >> romanNum;
    cout << endl;
    
    r.setRoman(romanNum);
    r.convertRoman(total);
         
    cout << "Do you want to see the [R]oman Numeral or the [D]ecimal? ";
    cin >> choice;
    cout << endl;
    
    if(choice == 'R' || choice == 'r')
          r.printRoman(romanNum);
    else if(choice == 'D' || choice == 'd')
          r.printDecimal(total);
    else
          cout << "That was not an invalid choice.." << endl;
    
    cout << endl;
    
    system("pause");
    return 0;   
}//End main

Here's what I have so far in the implementation file:

//  romanTypeImp.cpp

#include <iostream>
#include "romanType.h"

using namespace std;

romanType::romanType()
{
                                
}//End romanType Constructor

romanType::romanType(char)
{
     char romanNum;              
}//End romanType(string) Constructor

romanType:: ~romanType()
{

}//End ~romanType Destructor

void romanType::setRoman(char)
{
     char romanNum;
}//End getRoman

int romanType::convertRoman(int& total)
{
     int len = strlen(romanNum);
     int count[len];
     
     for(int i = 0; i > len; i++)
     {           
           switch(romanNum[i])
           {
                 case 'M':
                      count[i] = 1000;
                      break;
                 case 'm':
                      count[i] = 1000;
                      break;
                 case 'D':
                      count[i] = 500;
                      break;
                 case 'd':
                      count[i] = 500;
                      break;
                 case 'C':
                      count[i] = 100;
                      break;
                 case 'c':
                      count[i] = 100;
                      break;
                 case 'L':
                      count[i] = 50;
                      break;
                 case 'l':
                      count[i] = 50;
                      break;
                 case 'X':
                      count[i] = 10;
                      break;
                 case 'x':
                      count[i] = 10;
                      break;
                 case 'V':
                      count[i] = 5;
                      break;
                 case 'v':
                      count[i] = 5;
                      break;
                 case 'I':
                      count[i] = 1;
                      break;
                 case 'i':
                      count[i] = 1;
                      break;
           }//End switch     
           total = total + count[i];
     }//End for
  
     total = 0;

     for (int i = 0; i < len-1; i++)
     {
          if(count[i] < count[i+1])
                 total = total - 2 * count[i];
          }//End for          
}//End convertRoman

void romanType::printRoman(char romanNum) const
{
     cout << "Here is your number displayed in Roman Numeral form: " << romanNum << endl;
}//End printRoman

int romanType::printDecimal(int& total)
{
     cout << "Here is your number displayed in Decimal form: " << total << endl;
}//End printDecimal

And here's what I have so far in the header file:

//romanType.h

using namespace std;

class romanType
{
public:
       void printRoman(char) const;
       int printDecimal(int&);
       void setRoman(char); 
       int convertRoman(int&);
       
       romanType();
       romanType(char);
       ~romanType();
        
private:
       char romanNum[6];
       int decimal;
       int total;
};//End Class romanType

If anyone can offer any guidance, that would be much appreciated! I apologize for my code being a mess.. I got frantic and started throwing things in to see if I could get something to work and now I don't know what to keep, what to lose, and what to change.

Thanks for any help you guys can offer!

Recommended Answers

All 6 Replies

Your romanNum in main is just one character. I'm assuming you're going to want to convert more than that.

setRoman is incorrect, you have no variable name for the parameter and it doesn't really do anything. If you are using C-style strings, look into functions in the <cstring> header like strcpy (copying one string to another) and strcmp (compare two strings).

void romanType::setRoman(string& roman)
{
romanNum = roman;
}//End getRoman

This is my new set function, my main concern is the roman numeral conversion to a decimal/number, I've been cleaning up my code quite a bit, but still this algorithm haunts me, I've searched online for quite awhile and based mine from a few others trying to piece something together while understanding whats going on underneath but I am not getting the required results, nor anything close. Do u have any suggestions?

Lines 83-89 are confusing, what are you trying to accomplish there?

That confuses me as well, that was a portion of code I was using to test because another algorithm for this conversion was using it, so I was seeing what it did and if it worked. As of now, that whole function is commented out as it doesn't work, So I am starting from scratch. My initial plan is to use the switch based on the roman numeral array as it is up top and then do the case as is also.

I would like the user to input, call the set function, and call the convert function and I want to use the case switch as I did before, but I am not sure how to read the string letter by letter and give each letter of input a value and output as such. I am assuming a for loop that includes string length of some sort?

Yes, you can access the characters of a string like array elements s[0] is the first character of string s, etc.

Read WaltP's explanation in pseudocode in http://www.daniweb.com/forums/post1461547.html#post1461547 (the rest of that thread may or may not be helpful, but it can give you some ideas).

Great thanks, I will give it a peek and see if I can get something workin!

Be a part of the DaniWeb community

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