#include <iostream>
using namespace std;
// romanType constructor
class romanType{
public:
romanType(char&);
int convert();
void print();
void get();
private:
int M, D, C, L, X, V, I;
char romanNumeral;
};
// Set up Roman Numerals
romanType::romanType(char &ch){
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
cout << ch << endl;
romanNumeral=ch;
}
// Convert and return value
int romanType::convert(){
if (romanNumeral == 'M')
{
return 1000;
}
else if(romanNumeral == 'D')
{
return 500;
}
else if(romanNumeral == 'C')
{
return 100;
}
else if(romanNumeral == 'L')
{
return 50;
}
else if(romanNumeral == 'X')
{
return 10;
}
else if(romanNumeral == 'V')
{
return 5;
}
else if(romanNumeral == 'I')
{
return 1;
}
return 0;
}
void romanType::print(){
cout << romanNumeral << endl;
}
void romanType::get(){
}
int main(){
char romanNumeral;
cout << "Please enter a number in Roman numerals to be converted: ";
cin >> romanNumeral;
romanType roman=romanType(romanNumeral);
cout << roman.convert() << endl;;
system("pause");
return 0;
}
Roman Numeral Converter
Page 1 of 110 Replies - 5174 Views - Last Post: 15 March 2009 - 12:58 PM
#1
Roman Numeral Converter
Posted 26 February 2009 - 11:43 AM
I'm trying to write a program that will take some roman numerals entered in by the user and then display the numeric values (see code below). I have run into a problem that I can't seem to fix though; when the user enters in a set of roman numerals (say MMIII which should equal 2003) only the first roman numeral gets converted (so one 1000 would display. Surely there is something I'm over looking. I appreciate any help.
Replies To: Roman Numeral Converter
#2
Re: Roman Numeral Converter
Posted 26 February 2009 - 11:48 AM
your romanType object is only constructed with one char, so it can only process one letter. You would need to use a string to make roman types and calculate the value of the entire statement from a string of chars instead of just one. You need to change the instance variable to a string
Hope this helps!
and welcome to DIC!
Hope this helps!
and welcome to DIC!
This post has been edited by crazyjugglerdrummer: 26 February 2009 - 11:50 AM
#3
Re: Roman Numeral Converter
Posted 26 February 2009 - 11:49 AM
Unless I'm mistaken: romanType roman=romanType(romanNumeral); causes it to return 1000
(thus "roman = 1000" not += 1000)
(thus "roman = 1000" not += 1000)
This post has been edited by Hyper: 26 February 2009 - 11:50 AM
#4
Re: Roman Numeral Converter
Posted 26 February 2009 - 12:59 PM
I tried both methods you two suggested and it still doesn't seem to work. When I change
romanType roman=romanType(romanNumeral);to
romanType roman+=romanType(romanNumeral);I get a couple errors. Also, when I try to change every occurance of char to a string I get errors as well (I made sure to include the string header). Still confused on what to do.
#5
Re: Roman Numeral Converter
Posted 26 February 2009 - 01:04 PM
I'll see what I can do, minute.
#7
Re: Roman Numeral Converter
Posted 26 February 2009 - 01:27 PM
OK! It works perfectly fine. Sorry for the short delay, I've never attempted to do anything like this before... It's kind of fun trying new stuff.
Works like a charm.. Enjoy!
Oh, and remember: study it, don't just copy and paste what I did...
If you have ANY questions about what's what or how it works, ask.
EDIT:
Somethings you could is actually improve it so it has mathmatically proper conversions. IV = "6" with this class.
Works like a charm.. Enjoy!
Oh, and remember: study it, don't just copy and paste what I did...
#include <iostream>
using namespace std;
class cRomanNumeral {
private:
int Decimal; /* The total sum of the conversion */
int argumentSize; /* Number of Roman Numerals to be converted */
char *romanNumeral; /* The individual Roman Numeral to be converted */
public:
cRomanNumeral(char []); /* The constructor */
void Convert(); /* The converter */
void display(); /* This isn't required, but it's fun to put it here */
};
cRomanNumeral::cRomanNumeral(char Digit[]) {
Decimal = 0;
romanNumeral = Digit;
argumentSize = sizeof(romanNumeral) / sizeof(romanNumeral[0]);
/* Convert the Roman Numeral digit to a base-10 decimal */
Convert();
}
void cRomanNumeral::Convert() {
for (int x = 0; x < argumentSize; x++) {
switch (romanNumeral[x]) {
case 'M': case 'm': Decimal += 1000; break;
case 'D': case 'd': Decimal += 500; break;
case 'C': case 'c': Decimal += 100; break;
case 'L': case 'l': Decimal += 50; break;
case 'X': case 'x': Decimal += 10; break;
case 'V': case 'v': Decimal += 5; break;
case 'I': case 'i': Decimal += 1; break;
}
}
return;
}
void cRomanNumeral::display() { printf("%d", Decimal); return; }
int main() {
char romanNumeral[15] = {'\0'};
cout << "Please enter a number in Roman numerals to be converted: ";
cin >> romanNumeral;
_flushall();
cRomanNumeral Roman(romanNumeral);
Roman.display();
cin.get();
return 0;
}
If you have ANY questions about what's what or how it works, ask.
EDIT:
Somethings you could is actually improve it so it has mathmatically proper conversions. IV = "6" with this class.
This post has been edited by Hyper: 26 February 2009 - 01:33 PM
#8
Re: Roman Numeral Converter
Posted 26 February 2009 - 08:18 PM
Wow, I must say, that is much more help than I ever expected to get. This was extremely helpful and I appreciate you taking time out of your day to do all of that. I'm looking over the code right now and it really is helpful. Thank you for that.
#9
Re: Roman Numeral Converter
Posted 26 February 2009 - 08:51 PM
You're very welcome.
I appreciate you using the little green button, and I hope you stick around the site for more questions... 'cause making (fixing?) that Roman Numeral converter was fun!
I appreciate you using the little green button, and I hope you stick around the site for more questions... 'cause making (fixing?) that Roman Numeral converter was fun!
#10
Re: Roman Numeral Converter
Posted 14 March 2009 - 10:33 PM
I just found this thread linked from another thread. I went through it and decided to see how the code Hyper posted worked. When I tried it, it did convert from roman numerals but it just added them together. Ex: IV. Normaly and properly this would be 4 (1 before 5), but when entered in the converter it is 6 (1 plus 5), because it only adds I (1) and V (5).
So I don't know it that is how the program was intended to work, but if it was, what would be a good way to go about making one convert it properly?
So I don't know it that is how the program was intended to work, but if it was, what would be a good way to go about making one convert it properly?
#11
Re: Roman Numeral Converter
Posted 14 March 2009 - 11:00 PM
They cover several different methods.
oopse should have really read the topic before commenting... sorry its really late here.
This post has been edited by NickDMax: 14 March 2009 - 11:01 PM
#12
Re: Roman Numeral Converter
Posted 15 March 2009 - 12:58 PM
Newfie Ken, Yes, the proper math isn't on it this (I fixed it and never posted the newer version).
You would simply have to account for all letters (real easy) and check for improper "conversions" (eg: IIIIII = 7) but I didn't write this to spoon-feed an answer, more or less to correct and show a single method it can be done. If I fixed all bugs, how would he learn or even have fun fixing it himself?
EDIT: Here's a snippet post containing that version (still incomplete): http://www.dreaminco...snippet3208.htm
case 'I': case 'i': Decimal += 1;
if (romanNumeral[x + 1] == 'v' || romanNumeral[x + 1] == 'V') { Decimal -= 2; }
if (romanNumeral[x + 1] == 'x' || romanNumeral[x + 1] == 'X') { Decimal -= 2; }
You would simply have to account for all letters (real easy) and check for improper "conversions" (eg: IIIIII = 7) but I didn't write this to spoon-feed an answer, more or less to correct and show a single method it can be done. If I fixed all bugs, how would he learn or even have fun fixing it himself?
EDIT: Here's a snippet post containing that version (still incomplete): http://www.dreaminco...snippet3208.htm
This post has been edited by Hyper: 15 March 2009 - 12:59 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|