I am having problems with my faction calculator. The problem is to create a menu for the calculator and to use classes for the fraction. The calculator should add, subtract, multiply, divide, reduce, and convert to decimal. Any help would be nice.
Fraction.cpp
#include "Fraction.h"
Fraction::Fraction(void){
num = 0;
den = 1;
}
Fraction::~Fraction(void){
cout << "Destroying fraction" << num << "/" << den << endl;
}
Fraction::Fraction(int n, int d){
num = n;
den = d;
}
void Fraction::print(ostream & out){
out << num << "/" << den;
}
void Fraction::plusEquals(const Fraction & second){
num = num * second.den + den * second.num;
den = den * second.den;
}
bool Fraction::scan(istream & in){
int n,d;
char slash;
in >> n >> slash >> d;
if(slash == '/'){
num = n;
den = d;
returntrue;
}
else{
returnfalse;
}
}
void Fraction::minusEquals(Fraction & second){
num = num * second.den - den * second.num;
den = den * second.den;
}
void Fraction::timesEquals(Fraction & second){
num = num * second.num;
den = den * second.den;
}
void Fraction::dividesEquals(Fraction & second){
num = num * second.den;
den = den * second.num;
}
void Fraction::reduce(void){
}
double Fraction::todecimal(void){
return 0;
}
#include "Fraction.h"
int main(){
int choice;
Fraction first;
Fraction second;
Fraction one;
cout << "----------------- Fraction Calculator - *********** -----------------\n";
cout << "1. Add two fractions\n";
cout << "2. Subtract two fractions\n";
cout << "3. Multiply two fractions\n";
cout << "4. Divide two fractions\n";
cout << "5. Reduce a fraction\n";
cout << "6. Convert a fraction to a decimal\n";
cout << "Enter your choice: ";
cin >> choice;
if(choice != 5 && choice !=6){
cout << "Enter the first fraction: ";
first.scan(cin);
cout << "Enter the second fraction: ";
second.scan(cin);
if(choice > 0){
switch(choice){
case'1':
first.plusEquals(second);
cout << "The sum of the two fractions is: ";
first.print(cout);
cout << endl;
break;
case'2':
first.minusEquals(second);
cout << "The difference of the two fractions is: ";
first.print(cout);
cout << endl;
break;
case'3':
first.timesEquals(second);
cout << "The product of the two fractions is: ";
first.print(cout);
cout << endl;
break;
case'4':
first.dividesEquals(second);
cout << "The quotient of the two fractions is: ";
first.print(cout);
cout << endl;
break;
default:
break;
}
}
}
else{
if(choice > 0){
cout << "Enter the fraction: ";
one.scan(cin);
switch(choice){
case'5':
one.reduce();
cout << "The reduced fraction is: ";
one.print(cout);
break;
case'6':
one.todecimal();
cout << "The fraction as a decimal is: ";
one.print(cout);
break;
default:
break;
}
}
}
}
When I use the switch, nothing is printed at all except for the destructor. Obviously I haven't finished my reduce or todecimal functions.