Class Functions Not Working

This is a discussion on Class Functions Not Working within the C++ Programming forums, part of the General Programming Boards category; I am taking a Data Structures using C++ class. This is my second program for this class, and I just ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Class Functions Not Working

    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!
    Last edited by clegs; 10-27-2007 at 05:01 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks ALL!
    But, I figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Class functions can't use const =/
    By neandrake in forum C++ Programming
    Replies: 10
    Last Post: 12-02-2004, 01:48 AM
  3. Defining Class memeber functions
    By silk.odyssey in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 06:50 PM
  4. Templated functions inside a class
    By Stan100 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 10:50 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21