0

Dear Friends:

I am struggling to overload the operators on this exercise. It was working fine when I just had the addition operator overloaded for the extRomanType but now I can't get them to work again. I have searched through the internet and I cannot figure out what I am doing wrong. Any assistance in this matter would be greatly appreciated.

Dani

/*

```
#include <iostream>
#include "Assignch2ex20.h"
#include <string>
#include <conio.h>
using namespace std;
int main()
string rmn, decVal;
int value,choice=0, deci;
tryAgain:
cout<< "Which would you like to convert?n";
cout<< "Type 1 for Roman Numeral to Decimal.n";
cout<< "Type 2 for Decimal to Roman Numeral.n";
cout<< "Type 3 for manipulating 2 Roman Numeral's (+,-,*,/).n";
cin>>choice;
if (choice==1){
cout << "Please enter the Roman Numeral to convert : ";
cin >> rmn;
RomanType myRomanType(rmn);
value = myRomanType.romanToDecimal();
cout << "The input Roman Numeral " << rmn << " is equal to the numerical " << value <<endl;
}
else if (choice==2){
negDec:
cout<< "Please enter a Decimal to convert less than 50000 : ";
cin>>deci;
if (deci<=0){cout<< "Please enter a number greater than 0.n";
goto negDec;}
if (deci>50000){cout<< "Please enter a number less than 50000.n";
goto negDec;}
RomanType myRomanType (deci);
decVal = myRomanType.decimalToRoman();
cout<<"The input decimal "<<deci<<" is equal to the Roman Numeral "<<decVal<<".n";
}
else if (choice==3){
string rmn1, rmn2;
int manip=0;
negNum:
cout<< "Please enter your first Roman Numeral: n ";
cin>>rmn1;
cout<< "Please enter your second Roman Numeral: n ";
cin>>rmn2;
RomanType rmncon1 (rmn1);
RomanType rmncon2 (rmn2);
cout<< "n Please enter your choice for manipulation.n";
cout<< "1-Additionn 2-Subtractionn 3. Multiplicationn 4. Divisionn";
cin>>manip;
if (manip==1){
extRomanType temp = rmncon1 + rmncon2;
cout<<" Result of the addition is: "<<temp.decimalToRoman()<<"n";
}
/* else if (manip==2){
if (rmn1<rmn2){
cout<<"The first number must be greater than the second to subtract. Please re-enter your numbers.n";
goto negNum;
}
extRomanType temp = rmncon1 - rmncon2;
cout<<" Result of the subtraction is: "<<temp.decimalToRoman()<<"n";
}
else if (manip==3){
extRomanType temp = rmncon1 * rmncon2;
cout<<" Result of the multiplication is: "<<temp.decimalToRoman()<<"n";
}
else if (manip==4){
if (rmn1<rmn2){
cout<<"The first number must be greater than the second to divide. Please re-enter your numbers.n";
goto negNum;
}
extRomanType temp = rmncon1 / rmncon2;
cout<<" Result of the division is: "<<temp.decimalToRoman()<<"n";
}
}
else{
cout<<"Incorrect choice please try again.n"<<endl;
goto tryAgain;}*/
getch();
Here is my scecond program:
#include <iostream>
#include <string>
using namespace std;
class RomanType
{
public:
string decCon;
int sum, total;
string get()
{
return romanType;
}
void show()
{
cout << "Roman Number is " << romanType << endl;
}
RomanType(const string &input)
{
romanType = input;
total = romanToDecimal();
}
RomanType(int &input)
{
sum = input;
decCon = decimalToRoman();
}
int romanToDecimal()
{
int length = romanType.length();
int previous = 0;
bool error = false;
int nIndex = 0;
sum = 0;
;
while( (error == false) && (nIndex < length) )
{
switch(romanType[nIndex])
{
case 'M':
sum += 1000;
if(previous < 1000)
{
sum -= 2 * previous;
}
previous = 1000;
break;
case 'D':
sum += 500;
if(previous < 500)
{
sum -= 2 * previous;
}
previous = 500;
break;
case 'C':
sum += 100;
if(previous < 100)
{
sum -= 2 * previous;
}
previous = 100;
break;
case 'L':
sum += 50;
if(previous < 50)
{
sum -= 2 * previous;
}
previous = 50;
break;
case 'X':
sum += 10;
if(previous < 10)
{
sum -= 2 * previous;
}
previous = 10;
break;
case 'V':
sum += 5;
if(previous < 5)
{
sum -= 2 * previous;
}
previous = 5;
break;
case 'I':
sum += 1;
if(previous < 1)
{
sum -= 2 * previous;
}
previous = 1;
break;
default:
cout << romanType[nIndex] << " is not a Roman Numeral!" << endl;
error = true;
sum = 0;
}
nIndex++;
}
return sum;
}
int length()
{
return romanType.length();
}
string decimalToRoman()
{
int i=0,decimal=sum;
while (decimal>=1000){decCon += "M",decimal-=1000,i++;}
while (decimal>=500){decCon+="D",decimal-=500,i++;}
while (decimal>=100){decCon+="C",decimal-=100,i++;}
while (decimal>=50){decCon+="L",decimal-=50,i++;}
while (decimal>=10){decCon+="X",decimal-=10,i++;}
while (decimal==9){decCon+="I",i++,decCon+="X",decimal-=9,i++;}
while (decimal>=5){decCon+="V",decimal-=5,i++;}
while (decimal==4){decCon+="I",i++,decCon+="V",decimal-=4,i++;}
while (decimal>=1){decCon+="I",decimal-=1,i++;}
return decCon;
};
protected:
string romanType;
int romanType2;
};
class extRomanType :public RomanType{
public:
extRomanType operator+ (RomanType param){
RomanType t("X");
t.total = total + param.total;
return t;
}
```

*Edited 3 Years Ago by daniel1977*