Thread: Roman Numeral Calculator

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Roman Numeral Calculator

    Beginning stages of a roman numeral to regular number calculator. This program assumes that the user is going to enter a valid roman numeral. Compiles fine, but numbers are always 1000 for some reason. Need a second pair of eyes on this:

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<string>
    #include<iomanip>
    #include<conio.h>
    
    #define I 1
    #define V 5
    #define X 10
    #define L 50
    #define C 100
    #define D 500
    #define M 1000
    
    using namespace std;
    
    class Roman
    {
    public:
    	//Function Prototypes
    	Roman(){total=0; counter=0;}
    	void menu(void);
    	bool is_ronumeral(const char&) const;
    	void calculate(char&);
    	void error_message(void);
    
    private:
    	//Globals
    	int total;
    	int counter;
    	string user_input;
    	int numarray[80];
    };
    
    int main()
    {
    	Roman numeral;
    
    	char choice;	
    
    	do{
    		system("cls");
    		
    		numeral.menu();
    
    		choice=toupper(getch());		
    
    		if(numeral.is_ronumeral(choice)  && choice!='Q')
    
    			numeral.calculate(choice);
    		
    		if(!numeral.is_ronumeral(choice) && choice!='Q')
    
    			numeral.error_message();
    
    	}while(choice!='Q');
    
    	system("cls");
    
    	return EXIT_SUCCESS;
    }
    
    void Roman::menu(void)
    {
    	cout << endl << endl
    		 << setw(10) << "***************************************"     << endl
    		 << setw(10) << "* Welcome to Roman Numeral Calcultor! *"     << endl
    		 << setw(10) << "***************************************\n"   << endl
    		 << setw(10) << "Enter your roman numerals one at a time"     << endl
    		 << setw(10) << "Enter 'Q' to Quit\n\n"
    		 << endl << endl
    		 << "\tNumerals Entered: " << user_input 
    		 << "\n\n\tCurrent Total: " << total
    		 << endl << endl << endl << endl << endl 
    		 << "\tEnter Roman Numeral: ";
    }
    
    bool Roman::is_ronumeral(const char& keydown) const
    {
    	switch(keydown)
    	{
    		case 'I':
    		case 'V':
    		case 'X':
    		case 'L':
    		case 'C':
    		case 'D':
    		case 'M': return true;		 
    
    		default:  return false;
    	}
    }
    
    void Roman::calculate(char& numeral) 
    {
    	user_input+=numeral;
    	user_input+=" ";
    
    	switch(numeral)
    	{
    		case 'I': numarray[counter]=I;
    		case 'V': numarray[counter]=V;
    		case 'X': numarray[counter]=X;
    		case 'L': numarray[counter]=L;
    		case 'C': numarray[counter]=C;
    		case 'D': numarray[counter]=D;
    		case 'M': numarray[counter]=M;
    	}
    
    	if(!counter)
    		total=numarray[counter];	
    
    	else
    	{
    		if(numarray[counter]<=numarray[counter-1])
    			total += numarray[counter];
    
    		if(numarray[counter]>numarray[counter-1])
    			total += (numarray[counter] - numarray[counter-1]);
    	}
    
    	++counter;
    }
    
    void Roman::error_message(void)
    {
    	cout << "\n\n\t\aThat is not a Roman Numeral!  Try Again.";	
    	cout << "\n\b\tPress [Enter] to continue...";
    	//cin.ignore(200,'n');
    	cin.get();
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I think you forgot breaks on the case statement

    EDIT
    Yep, that was the problem
    Last edited by MadCow257; 04-13-2005 at 06:11 PM.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    good call

    Code:
    void Roman::calculate(char& numeral) 
    {
    	user_input+=numeral;
    	user_input+=" ";
    
    	switch(numeral)
    	{
    		case 'I': numarray[counter]=I;
    			  break;
    		case 'V': numarray[counter]=V;
    			  break;
    		case 'X': numarray[counter]=X;
    			  break;
    		case 'L': numarray[counter]=L;
    			  break;
    		case 'C': numarray[counter]=C;
    			  break;
    		case 'D': numarray[counter]=D;
    			  break;
    		case 'M': numarray[counter]=M;
                              break;			     
    	}
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Think I got the final program here:

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<string>
    #include<iomanip>
    #include<conio.h>
    
    #define I 1
    #define V 5
    #define X 10
    #define L 50
    #define C 100
    #define D 500
    #define M 1000
    
    using namespace std;
    
    class Roman
    {
    public:
    	//Function Prototypes
    	Roman(){total=0; counter=0;}
    	void menu(void);
    	bool is_ronumeral(const char&) const;
    	void calculate(char&);
    	void error_message(void);
    
    private:
    	//Globals
    	int total;
    	int counter;
    	string user_input;
    	int numarray[80];
    };
    
    int main()
    {
    	Roman numeral;
    
    	char choice;	
    
    	do{
    		system("cls");
    		
    		numeral.menu();
    
    		choice=toupper(getch());		
    
    		if(numeral.is_ronumeral(choice)  && choice!='Q')
    
    			numeral.calculate(choice);
    		
    		if(!numeral.is_ronumeral(choice) && choice!='Q')
    
    			numeral.error_message();
    
    	}while(choice!='Q');
    
    	system("cls");
    
    	return EXIT_SUCCESS;
    }
    
    void Roman::menu(void)
    {
    	cout << endl << endl
    		 << setw(10) << "***************************************"     << endl
    		 << setw(10) << "* Welcome to Roman Numeral Calcultor! *"     << endl
    		 << setw(10) << "***************************************\n"   << endl
    		 << setw(10) << "Enter your roman numerals one at a time"     << endl
    		 << setw(10) << "Enter 'Q' to Quit\n\n"
    		 << endl << endl
    		 << "\tNumerals Entered: " << user_input 
    		 << "\n\n\tCurrent Total: " << total
    		 << endl << endl << endl << endl << endl 
    		 << "\tEnter Roman Numeral: ";
    }
    
    bool Roman::is_ronumeral(const char& keydown) const
    {
    	switch(keydown)
    	{
    		case 'I':
    		case 'V':
    		case 'X':
    		case 'L':
    		case 'C':
    		case 'D':
    		case 'M': return true;		 
    
    		default : return false;
    	}
    }
    
    void Roman::calculate(char& numeral) 
    {
    	user_input+=numeral;
    	
    	switch(numeral)
    	{
    		case 'I': numarray[counter]=I;
    			      break;
    		case 'V': numarray[counter]=V;
    			      break;
    		case 'X': numarray[counter]=X;
    			      break;
    		case 'L': numarray[counter]=L;
    			      break;
    		case 'C': numarray[counter]=C;
    			      break;
    		case 'D': numarray[counter]=D;
    			      break;
    		case 'M': numarray[counter]=M;			     
    	}
    
    	if(!counter)
    		total=numarray[counter];	
    
    	else
    	{
    		if(numarray[counter]<=numarray[counter-1])
    			total += numarray[counter];
    
    		if(numarray[counter]>numarray[counter-1])
    			total += (numarray[counter] - (2 * numarray[counter-1]));
    	}
    
    	++counter;
    }
    
    void Roman::error_message(void)
    {
    	cout << "\n\n\t\aThat is not a Roman Numeral!  Try Again.";	
    	cout << "\n\tPress [Enter] to continue...";
    	cin.get();
    }
    Last edited by The Brain; 04-14-2005 at 05:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  2. roman numeral conversion
    By clarky101 in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 07:13 PM
  3. Roman Numeral...T_T
    By only1st in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 10:06 AM
  4. Roman Numeral Convertor
    By mike1000 in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 02:53 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM