GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Aug-2005, 08:02
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road

Frustrating C++ problem... Roman numerals


I've been trying to wrap my head around this one for the past few days... to start, I'm not all that good at programming, so I'm probably just forgetting minor things I'm supposed to know by now. But anyway.... I've been assigned a program that will take either arabic or roman numerals and spit roman numerals back out. That much I could do. However we're required to use a class that was designed for us, and I can't for the life of me figure out how to go about it. All we got to start was the class below (minus the result1-4 ints. Those were my doing)

1. complete the methods.
2. verify that the "conditions maintained in class" is correct.
3. write an interface to the class that will allow the user to enter arabic and Roman Numerals, and return roman numerals.
4. complete the documentation

You may find it helpful to consider the use of strcpy and strcat in cstring. The help in initializing and building strings. The data type char* is a string represented as an array of character.
*/






CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>
#include <cstring>

class romanNumerals {
// handles arithmetic operations on Roman Numerals
// conditions maintained in class:
	//   0 < num <= 3999
	public:
		void returnRoman (char roman[]);	// returns a roman numeral
		void setArabic (int number);	// accepts an integer
		void setRoman (char* roman);	// accepts a roman numeral
		bool isError ();		// returns true if the most recent operation has caused an error.
	private:
		int result1, result2, result3, result4;
		int num;		// the internal representation of the roman numeral
				          // the actual roman numeral is not maintained
}; // class romanNumerals




void romanNumerals::returnRoman(char roman[]){
	
	cout<<"The Roman Equivelant is:"<<roman;
}

void romanNumerals::setArabic(int number){
		num=number;
		if  ( num < 0 || num >=4000)// Check to see if the number entered is less than 3999 and greater than 1
		{
			cout<<"Invalid input"<<endl;    //Prints out an error message
		}
		else{
char *t[]={ "M", "MM", "MMM"};
char *h[]= { "C", "CC", "CCC", "CD", "D" ,"DC", "DCC", "DCCC", "CM"};
char *ten[]={ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
char *one[]={"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

			

			if (num >= 1000  && num < 4000){ //Obtains the value of thousands
				result1 = num /1000;
				
				num=num-(result1*1000);
				 
			}
			if (num < 1000 && num >=100){ //Obtains the value of hundreds
				 result2 = num / 100;
				 
				 num=num-(result2*100);
			}
			if (num < 100 && num>= 10){  //Obtains the value of tens
				result3 = num /10;
				
	            num=num-(result3*10);
			}
			if (num <10 && num >= 1){ //Obtains the value of ones
		         result4=num;
				 
			}
			
		
		
		}
}

void romanNumerals::setRoman(char* roman){

	returnRoman(roman);

		
}





int main(){
	romanNumerals test;
	int number=0, menu=0;
	char* roman;

	cout<<"Would you like to enter input as \n1.Arabic \n2.Roman\n";
	cin>>menu;

	if(menu==1){
		cout<<"Please enter an Arabic number between 1 and 3999\n";
		cin>>number;
		test.setArabic(number);
	}
	else if(menu==2){
		cout<<"Please enter a Roman Numeral between 1 and 3999\n";
		cin>>roman;
		test.setRoman(roman);
	}
	else{
		cout<<"Invalid menu selection.  Please try again.";
	}

	return 0;
}


That's what I have so far... I'm sure it's a mess, but I got to a point where all I could do was get it to compile, even though it doesn't do anything. I don't know how to work with strings really (arrays I can play with a little), so how to convert the arabic number to roman numerals, or how to even get the roman numeral input option to do anything is freezing me up.

If anyone could give me some idea how to fix this I'd be grateful.
  #2  
Old 29-Aug-2005, 11:33
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I assume this was the class you were given:
CPP / C++ / C Code:
class romanNumerals {
// handles arithmetic operations on Roman Numerals
// conditions maintained in class:
	//   0 < num <= 3999
	public:
		void returnRoman (char roman[]);	// returns a roman numeral
		void setArabic (int number);	// accepts an integer
		void setRoman (char* roman);	// accepts a roman numeral
		bool isError ();		// returns true if the most recent operation has caused an error.
	private:
		int num;		// the internal representation of the roman numeral
				          // the actual roman numeral is not maintained
}; // class romanNumerals
The methods setArabic () and setRoman() are your input methods. Notice the comment // accepts a(n) ....
All they do is get your input
returnRoman() is the method that computes the Roman numeral and passes it back to main() to be output.

So start by rearranging your program to get the proper functionality. And sprinkle couts at key places to see how your conversion is progressing.

Oh, and thank you for reading the Guidelines first! It really helps
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 29-Aug-2005, 12:04
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
Thanks. Most of my confusion stemmed from the fact that the professor left a lab while sick, so he wasn't able to explain it and the professor standing in had a hard time explaining it himself. Now that he's back he explained it a little better in class and I have a better idea of what I'm supposed to do. My main confusion at this point is the arrays and strings (I believe there's one of each). I understand the concepts of what they do, but how to declare them, convert them, copy them, etc. I understand the logic now, I just can't find the code that makes it work (the book's a little helpful, but doesn't have nearly enough examples for the way I learn). The logic itself I think I've finally picked up on at least.
  #4  
Old 30-Aug-2005, 08:39
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
Ok, here's what I've got now that I have a little more direction. My strcats in return roman don't seem to be working, I only get the thousands place when I check it with cout. And as for how to convert roman numeral input to an integer I'm completely lost. Definitely better off than I was though.

CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>
#include <cstring>
#include <stdlib.h>

class romanNumerals {
// handles arithmetic operations on Roman Numerals
// conditions maintained in class:
	//   0 < num < 3999
	public:
		void returnRoman (char roman[]);	// returns a roman numeral
		void setArabic (int number);	// accepts an integer
		void setRoman (char* roman);	// accepts a roman numeral
		bool isError ();		// returns true if the most recent operation has caused an error.
	private:
		int num;		// the internal representation of the roman numeral
								// the actual roman numeral is not maintained
}; // class romanNumerals

void romanNumerals::returnRoman(char roman[]){

	int temp, temp2, temp3, temp4;

	char *thou[]={ "M", "MM", "MMM"};
	char *hun[]= { "C", "CC", "CCC", "CD", "D" ,"DC", "DCC", "DCCC", "CM"};
	char *ten[]={ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
	char *one[]={"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
	roman[0]=0;
		
if  ( num < 0 || num >=4000)// Checkt to see if the number entered is Less than 3999 and greater than 1
	{
		//Prints out an error message
	}
	else{

		if (num >= 1000  && num < 4000){ //Obtains the value of thousands
			temp = num/1000;
			strcat ( roman, thou[temp-1] );
		}
		if (num < 1000 && num >=100){ //Obtains the value of hundreds
			temp2 = (num-(temp*1000))/100;
			strcat ( roman, hun[temp2-1] );
		}
		if (num < 100 && num>= 10){  //Obtains the value of tens
			temp3 = num-(temp*1000)-(temp2*100)/10;
			strcat ( roman, ten[temp3-1] );
		}
		if (num <10 && num >= 1){ //Obtains the value of ones
			temp4 = num-(temp*1000)-(temp2*100)-(temp3*10);
			strcat ( roman, one[temp4-1] );
		}
		       
		}
cout<<roman<<endl;

}

void romanNumerals::setArabic(int number){
	num=number;
}

void romanNumerals::setRoman(char* roman){
	
}

//bool romanNumerals::isError(){
//return 1;
//}


int main(){
	romanNumerals check;
	int menu=0, number;
	char romanInput[20];
	
	cout<<"Would you like to enter input as \n1.Arabic \n2.Roman\n";
	cin>>menu;

	if(menu==1){
		cout<<"Please enter an Arabic number between 1 and 3999\n";
		cin>>number;
		check.setArabic(number);
		check.returnRoman(romanInput);
	}
	else if(menu==2){
		cout<<"Please enter a Roman Numeral between 1 and 3999\n";
		cin.getline(romanInput, 20);
		check.setRoman(romanInput);
	}
	else{
		//error
	}
	return 0;
}
  #5  
Old 30-Aug-2005, 14:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
So I take it
CPP / C++ / C Code:
void setArabic (int number);  // accepts an integer
does not really mean
Code:
Call the setArabic() method Accept arabic number Return arabic number
because you're still doing it in main() and you have no worthwhile code in the method? "accepts an integer" to me means the method accepts/reads in/gets from keyboard the number.

Put couts inside the if's to see what values are being genereated, and to make sure you are actually going into the if's. You also might want to try using the modulus (%) operator instead of those equations. It will be simpler.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 30-Aug-2005, 15:30
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
It may mean to do the cin within the method. But there's not supposed to be anything else in there if I understood him correctly in our lab. It's supposed to be the simplest method, and as the lab I believe will be expanded on later. I will make that change though.

As for the ifs, it only appears to be going into 2 of them. Before this point it was giving me gibberish because I hadn't set it as anything, so initializing it as zero fixed that. But that part of the program I THINK I can make work myself. If I don't get that particular way of doing it to work I can always get it to pull the numerals another way and add them to the string, but I have a few things to try. The only thing I need to know is if my use of strcat is correct so I know I'm at least adding the numerals correctly, even if other parts of the code are interfering with it happening. If it's something else I can probably find it, but strcat is new to me.

That leaves me with setRoman. cin.getline seems to not be working (missing include on my part maybe? I've never used it before) so no input options come up when I attempt to enter a roman numeral. Should I get that working I assume I can read the array one item at a time and if I need to use if/elses to convert that to an integer value. Might be long, but it would get the job done I think... Would that work?
  #7  
Old 30-Aug-2005, 17:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Elsydeon
The only thing I need to know is if my use of strcat is correct so I know I'm at least adding the numerals correctly, even if other parts of the code are interfering with it happening. If it's something else I can probably find it, but strcat is new to me.
You cleared the buffer roman at the top of the method. Good.
Your syntax for strcat() is fine as long as your temp values are never 0. It looks like they cannot be so that's good.

Summary: Yep, your strcat() is fine!

Quote:
Originally Posted by Elsydeon
That leaves me with setRoman. cin.getline seems to not be working (missing include on my part maybe? I've never used it before) so no input options come up when I attempt to enter a roman numeral.
No, you don't need another include, although I noticed you'r using the includes from 10 years ago. The current C++ prefers the includes without the .h extension. Search this board for more info, I don't wish to repeat what's already been explained hundreds of times.

Quote:
Originally Posted by Elsydeon
Should I get that working I assume I can read the array one item at a time and if I need to use if/elses to convert that to an integer value. Might be long, but it would get the job done I think... Would that work?
Yeah, that can work. You just have to keep track of the value's magnitude to know whether to add or subtract the previous value like in CM, XL, etc. That's the toughest part. I have some ideas but you play with it first and see what you come up with.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 30-Aug-2005, 20:13
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
I'll likely post one more update early in the morning. Lab is due tomorrow afternoon, so hopefully I'll make some more progress and be able to clear up little bugs, but I think I'm at least on my way to some sort of functional product. I appreciate the help a lot. Being completely honest I've gotten this far into my school's Computer Science curriculum because of the weight of the tests vs the labs. I understand concepts perfectly fine, implementing them is more my weakness. I'm by no means a programmer, had never touched it before going to college. And it's the first thing we get thrown into (I suppose to weed out the undedicated) so it's definitely been an uphill battle. But at least I'm stubborn enough to work on it when it's not really my thing, so hopefully I'll pull through.

Again, I appreciate the help, hopefully I might even have a finished product to post tomorrow, heh.
  #9  
Old 31-Aug-2005, 09:00
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
Some slight changes. I got my returnRoman method working, which was about half the battle. I'm pretty sure I know what to do to get setRoman going, but I can't get the string of input from the user. cin.getline wouldn't work, and someone reccomended I use gets, but that seems to be giving me problems too. It's my last real problem at least, and that's something. And hey, I have output.

(I'll be removing the couts in the methods I've already finished soon enough.)

CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>
#include <cstring>
#include <stdlib.h>

class romanNumerals {
// handles arithmetic operations on Roman Numerals
// conditions maintained in class:
	//   0 < num < 3999
	public:
		void returnRoman (char roman[]);	// returns a roman numeral
		void setArabic (int number);	// accepts an integer
		void setRoman (char* roman);	// accepts a roman numeral
		bool isError ();		// returns true if the most recent operation has caused an error.
	private:
		int num;		// the internal representation of the roman numeral
								// the actual roman numeral is not maintained
}; // class romanNumerals

void romanNumerals::returnRoman(char roman[]){

	int temp, temp2=num;
	roman[0]=0;
	char *thou[]={ "M", "MM", "MMM"};
	char *hun[]= { "C", "CC", "CCC", "CD", "D" ,"DC", "DCC", "DCCC", "CM"};
	char *ten[]={ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
	char *one[]={"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
	int count;
		
if  ( num < 0 || num >=4000)// Checkt to see if the number entered is Less than 3999 and greater than 1
	{
		//Prints out an error message
	}
	else{

		for(count=0; count<4; count++){

		if (temp2 >= 1000  && temp2 < 4000){ //Obtains the value of thousands
			temp = num/1000;
			temp2 = num%1000;
			strcat ( roman, thou[temp-1] );
			cout<<roman<<endl;
		}
		if (temp2 < 1000 && temp2 >=100){ //Obtains the value of hundreds
			temp = (num%1000)/100;
			temp2 = num%100;
			strcat ( roman, hun[temp-1] );
			cout<<roman<<endl;
		}
		if (temp2 < 100 && temp2>= 10){  //Obtains the value of tens
			temp = ((num%1000)%100)/10;
			temp2 = num%10;
			strcat ( roman, ten[temp-1] );
			cout<<roman<<endl;
		}
		if (temp2 <10 && temp2 >= 1){ //Obtains the value of ones
			temp = (((num%1000)%100)%10);
			temp2=0;
			strcat ( roman, one[temp-1] );
			cout<<roman<<endl;
		}
		}
		       
		}


}

void romanNumerals::setArabic(int number){
	num=number;
}

void romanNumerals::setRoman(char* roman){
int j=0;
num = 0;
while (roman[j] == 'M')
 {
 j++;
 num+=1000;
 }
j=0;
if (roman[j] == 'C' && roman[j+1] == 'M')
 num+=900;
}

//bool romanNumerals::isError(){
//return 1;
//}


int main(){
	romanNumerals check;
	int menu=0, number;
	char romanInput[20];
	
	cout<<"Would you like to enter input as \n1.Arabic \n2.Roman\n";
	cin>>menu;

	if(menu==1){
		cout<<"Please enter an Arabic number between 1 and 3999\n";
		cin>>number;
		check.setArabic(number);
		check.returnRoman(romanInput);
	}
	else if(menu==2){
		cout<<"Please enter a Roman Numeral between 1 and 3999\n";
//Get Input with getstring somehow.  Call setRoman.
	}
	else{
		//error
	}
	return 0;
}

I'll be searching the board for the answer I suppose. Just thought I'd give a progress update. Oh, and does that method of converting the roman numeral to integer look sound once I get it going?
  #10  
Old 31-Aug-2005, 11:14
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Elsydeon
Some slight changes. I got my returnRoman method working, which was about half the battle. I'm pretty sure I know what to do to get setRoman going, but I can't get the string of input from the user. cin.getline wouldn't work, and someone reccomended I use gets, but that seems to be giving me problems too. It's my last real problem at least, and that's something. And hey, I have output.
Never never never never use gets()!!! I direct your attention back to "Things to avoid" for why. So use cin. For your programming level, that should suffice nicely for this task.
CPP / C++ / C Code:
cin >> romanInput;


Quote:
Originally Posted by Elsydeon
Oh, and does that method of converting the roman numeral to integer look sound once I get it going?
Not really. You want to accumulate the value of each individual character in a temp value. Every time you get a character that's different from the previous, add/subtract that temp value from the total and start over accumulating with the new character.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03
String problem vaha C Programming Language 3 24-May-2005 19:21
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.