I am taking a Data Structures using C++ class. This is my second program for this class, and I just finished 46 programs in my C++ class.
The chapter covered Classes, the basics only.
The program is to convert the roman number into an Arabic (decimal) number.
The progam consists of a class.
That should:
-Store the number as a roman numeral.
-Convert and store the number as a decimal.
-Print the number as a roman or decimal number as requested by the user.
And there are three roman numerals to test the code.
Everything I here compiles, but doesn't give the right result. I only get one roman numeral character to print and the arabic equivalent is the first two characters plus the previous amount.
I have gone cross-eyed looking at this, and can't see what the problem might be.
I would appreciate a few more peepers to take a look.
My "romanType.h" file
Code:
#include <string>
using namespace std;
class romanType
{
public:
romanType();
//Default Constructor
romanType(string roman);
//Constructor
void storeRomanNumber(string roman);
//Function to store the roman numeral
//Checks to see if all characters in the string are valid
void convertRomanNumber();
//Function to convert the Roman Number into and equivalent Arabic Number
void printRomanNumber();
//Prints the Roman Number as entered by the user
void printArabicNumber();
//Prints the converted Arabic equivalent number
private:
int romanToArabic;
string romanNumeral;
};
My romanType.cpp" file - with the function definitions for my class
Code:
#include "romanType.h"
romanType::romanType()
{
romanNumeral = 'O';
romanToArabic = 0;
}
romanType::romanType(string roman)
{
storeRomanNumber(roman);
}
void romanType::storeRomanNumber(string roman)
{
for (int i = 0; i < roman.size(); i++)
if (islower(roman[i]))
roman[i] = toupper(roman[i]);
for (int i = 0; i < roman.size(); i++)
{
if (roman[i] == 'M' ||
roman[i] == 'D' ||
roman[i] == 'C' ||
roman[i] == 'L' ||
roman[i] == 'X' ||
roman[i] == 'V' ||
roman[i] == 'I')
romanNumeral[i] = roman[i];
else
romanNumeral[i] = 'N';
}
}// end storeRomanNumber
void romanType::convertRomanNumber()
{
int a = 0,
z = 0,
minusMaybe = 0;
for (int i = romanNumeral.size(); i >= 0; i--)
{
if (romanNumeral[i] == 'M') z = 1000;
if (romanNumeral[i] == 'D') z = 500;
if (romanNumeral[i] == 'C') z = 100;
if (romanNumeral[i] == 'L') z = 50;
if (romanNumeral[i] == 'X') z = 10;
if (romanNumeral[i] == 'V') z = 5;
if (romanNumeral[i] == 'I') z = 1;
if (z < a) minusMaybe = -1;
else minusMaybe = 1;
romanToArabic = romanToArabic + (z * minusMaybe);
a = z;
}// end for
}// end convertRomanNumber
void romanType::printRomanNumber()
{
cout << romanNumeral << endl;
}
void romanType::printArabicNumber()
{
cout << romanToArabic << endl;
}
and, finally my "romanNumeralMain.cpp" file
Code:
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
void displayBanner();
void displayQuestion();
int main()
{
romanType romanNum;
char userSelection;
string roman;
displayBanner();
romanNum.storeRomanNumber("mcxiv");
romanNum.convertRomanNumber();
cout << "\nThe Roman numeral: ";
romanNum.printRomanNumber();
cout << "is equivalent to: ";
romanNum.printArabicNumber();
cout << endl;
romanNum.storeRomanNumber("ccclix");
romanNum.convertRomanNumber();
cout << "\nThe Roman numeral: ";
romanNum.printRomanNumber();
cout << "is equivalent to: ";
romanNum.printArabicNumber();
cout << endl;
romanNum.storeRomanNumber("MDCLXVI");
romanNum.convertRomanNumber();
cout << "\nThe Roman numeral: ";
romanNum.printRomanNumber();
cout << "is equivalent to: ";
romanNum.printArabicNumber();
cout << endl;
displayQuestion();
cin >> userSelection;
while (userSelection == 'Y' || userSelection == 'y')
{
cout << "\nEnter a Roman numeral: ";
cin >> roman;
cout << "\nThe Roman numeral you entered is: ";
romanNum.printRomanNumber();
romanNum.storeRomanNumber(roman);
cout << "\nThe Arabic equivalent of this Roman numeral is: ";
romanNum.convertRomanNumber();
romanNum.printArabicNumber();
cout << endl;
displayQuestion();
cin >> userSelection;
}// end while
}// end main
void displayBanner()
{
cout << "\nThis program allows you to enter a Roman numeral;"
<< "\nit then converts the string of characters into"
<< "\nthe equivalent Arabic number.\nHere are some examples:";
}
void displayQuestion()
{
cout << "\nThe Roman to Arabic conversion is:"
<< "\n\tM = 1000\n\tD = 500\n\tC = 100\n\tL = 50\n\tX = 10\n\tV = 5\n\tI = 1\n"
<< "\nWould you like to enter a Roman numeral to convert?" << endl;
}
Thanks for the help if anyone can!