0

hi everyone

I am having a little problem with the program below.

If I input "2/5 * 2/6" the char "/6" will be stored, so when I try to multiply by a simple variable by entering "3/7 * 6" the program will read "3/7 * 6/6".

is there a way to prevent this and if possible can you guys and or girls show me a work around?

Thank you in advance.

P.S. the complete code is below. so if you want, you can copy and compile it to see what I mean.

```
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
class Rational
{
private:
int num;
int den;
public:
// Accessor and mutator functions
void setNumerator(int);
void setDenominator(int);
int getNumerator();
int getDenominator();
// Greatest common divisor, needed to simplify fractions
int gcd();
// Simplifies fractions
void simplify();
Rational operator*(Rational);
};
int main()
{
int option;
int ratNum1, ratNum2, ratDen1, ratDen2 = 0;
char ope1, ope2, ope3;
cout << "Rational numbers calculator " << endl;
do
{
cout << "Main Menu\n";
cout << "1.To enter an operation\n";
cout << "2.To display the last result in decimal format\n";
cout << "Press 0 to quit.\n\n";
cout << "Please make a choice: ";
cin >> option;
cout << "\n";
if (option == 1)
{
cout << "Please enter an operation: \n";
cin >> ratNum1 >> ope1 >> ratDen1 >> ope2 >> ratNum2;
if (cin.peek() == '/')
{
cin >> ope3 >> ratDen2;
}
cout << ratNum1 << ope1 << ratDen1 << ope2 << ratNum2 << ope3 << ratDen2;
cout << "\n\n";
if (ope2 == '*' && ratDen2 == 0)
{
Rational rat1, rat2;
rat1.setNumerator(ratNum1);
rat1.setDenominator(ratDen1);
rat2.setNumerator(ratNum2);
rat2.setDenominator(ratDen2 = 1);
Rational rat3 = rat1 * rat2;
rat3.simplify();
cout << "The result of this operation is: " << rat3.getNumerator() << "/" << rat3.getDenominator() << " this is single" <<"\n\n";
}
if (ope2 == '*' && ratDen2 != 0 && ope3 == '/')
{
//delete[] ope3, *ratDen2;
Rational rat1, rat2;
rat1.setNumerator(ratNum1);
rat1.setDenominator(ratDen1);
rat2.setNumerator(ratNum2);
rat2.setDenominator(ratDen2);
Rational rat3 = rat1 * rat2;
rat3.simplify();
cout << "The result of this operation is: " << rat3.getNumerator() << "/" << rat3.getDenominator() << " this is rational"<< "\n\n";
}
}
if (option == 2)
{
if (ope2 == '*' && ratDen2 == 0)
{
Rational rat1, rat2;
rat1.setNumerator(ratNum1);
rat1.setDenominator(ratDen1);
rat2.setNumerator(ratNum2);
rat2.setDenominator(ratDen2=1);
Rational rat3 = rat1 * rat2;
rat3.simplify();
cout << "The result in decimal format is: "
<< (double(rat1.getNumerator()*rat2.getNumerator()))/(double(rat1.getDenominator()*1)) << "\n\n";
}
if (ope2 == '*' && ratDen2 != 0)
{
//delete[] ope3, *ratDen2;
Rational rat1, rat2;
rat1.setNumerator(ratNum1);
rat1.setDenominator(ratDen1);
rat2.setNumerator(ratNum2);
rat2.setDenominator(ratDen2);
Rational rat3 = rat1 * rat2;
rat3.simplify();
cout << "The result in decimal format is: "
<< (double(rat1.getNumerator()*rat2.getNumerator())/double(rat1.getDenominator()*rat2.getDenominator())) << "\n\n";
}
}
if (option != 1 && option != 2 && option != 0)
{
cout << "This is a wrong choice, please choose again... \n\n";
}
} while (option != 0);
cout << "Now quitting..." << endl;
system("PAUSE");
return 0;
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void Rational::setNumerator(int n)
{
num = n;
}
void Rational::setDenominator(int n)
{
den = n;
}
int Rational::getNumerator()
{
return num;
}
int Rational::getDenominator()
{
return den;
}
// Greatest common divisor
int Rational::gcd()
{
int a = num;
int b = den;
int tmp;
// While b is not 0
while (b)
{
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
// Simplifies the fraction
void Rational::simplify()
{
// Get the greatest common divisor
int gcdNum = gcd();
// If there is a common divisor, we don't want to divide by 0
if(gcdNum != 0)
{
// Set the new numerator
num = num / gcdNum;
// Set the new denominator
den = den / gcdNum;
}
}
Rational Rational::operator*(Rational ratPassed)
{
// Multiplying fractions
Rational ratResult;
ratResult.num = num * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
```

*Edited 4 Years Ago by alsz*: format