Fraction class program not working? Could use some help!

Hi, everyone! So I have a program I am doing for a CS course. I have pasted the .h and .cpp's below. It's not really calculating the 2 fractions I input. Could you please tell me what's up with my code? I appreciate all of your help!

P.S. Please do not try to change parameters, etc. I like the format I'm using (just recently started C++). Would you just help me correctly define what I'm trying to do?

class Fraction
{
public:

friend void print(Fraction f);
Fraction(); // default constructor
// Set numerator = 0, denominator = 1.

Fraction(int n, int d = 1); // Initializing constructor; Set numerator = n,
// denominator = d else default to 1
~Fraction(); // destructor
void Get(); // Get a fraction from keyboard.
void Show(); // Display a fraction on screen

double Evaluate(); // Return the decimal value of a fraction

// operator overloads, so we can do fractional arithmetic
// using a familiar operator. The left operand is this object;
// the right operand is f1.

Fraction operator+ (Fraction f1); // addition overload
Fraction operator- (Fraction f1); // subtraction overload
Fraction operator* (Fraction f1); // multiplication overload
Fraction operator/ (Fraction f1); // division overload


private:
void Lowest_Terms(); // reduces this fraction to lowest terms
int numerator; // top part
int denominator; // bottom part
};





#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include "fraction.h"
using namespace std;

void print(Fraction f) { //huh?
Fraction();
cout << f.numerator << "/" << f.denominator << "." << endl;
}

Fraction::Fraction() {
numerator = 0;
denominator = 1;
}

Fraction::Fraction(int n, int d) {
numerator = n;
if (denominator !=0) {
denominator = d;
}
else {
d = 1;
}
}

Fraction::~Fraction() {
}

void Fraction::Get() { //needs work
char slash;
cin >> numerator >> slash >> denominator;
}

void Fraction::Show() {
cout << numerator << "/" << denominator << endl;
}

double Fraction::Evaluate() {
double n = numerator;
double d = denominator;
return (n / d);

}

Fraction Fraction::operator+ (Fraction f1) {
Fraction local;
local.numerator = numerator * f1.denominator + denominator * f1.numerator;
local.denominator = denominator * f1.denominator;
local.Lowest_Terms();
return local;
local.Show();
}

Fraction Fraction::operator- (Fraction f1) {
Fraction local;
local.numerator = numerator * f1.denominator + denominator * f1.numerator;
local.denominator = denominator * f1.denominator;
local.Lowest_Terms();
return local;
Show();
}

Fraction Fraction::operator* (Fraction f1) {
Fraction local;
local.numerator = numerator * f1.numerator;
local.denominator = denominator * f1.denominator;
local.Lowest_Terms();
return local;
Show();
}

Fraction Fraction::operator/ (Fraction f1) {
Fraction local;
local.numerator = numerator * f1.denominator;
local.denominator = denominator * f1.numerator;
local.Lowest_Terms();
return local;
Show();
}

void Fraction::Lowest_Terms() {
int n = numerator;
int d = denominator;
int r;
while ((r = n % d) != 0) {
n = d;
d = r;
}
numerator /= d;
denominator /= d;
}



#include <iostream>
#include <iomanip>
#include <cstring> //might take out (for others also)
#include <cctype>
#include <cstdlib> //might take out (for others also)
#include "fraction.h"
using namespace std;

int main() {
Fraction f1;
Fraction f2;
Fraction f3;
char ch;

cout << "*** The Fraction Workout Program ***" << endl << endl;
cout << "The initialized fraction f3 is: ";
f3.Show();
// print(Fraction f);
cout << endl; //might need to remove

do {
cout << "Enter a fraction in the format-> integer/integer: ";
f1.Get();
cout << "The fraction assigned to f1 is: ";
f1.Show();
cout << endl;
cout << "Enter another fraction in the format-> integer/integer: ";
f2.Get();
cout << "The fraction assigned to f2 is: ";
f2.Show();
cout << endl;

do {
do {
cout << "Menu of Operations for f1 & f2: " << endl;
cout << "A: Addition" << endl;
cout << "S: Subtraction" << endl;
cout << "M: Multiplication" << endl;
cout << "D: Division" << endl;
cout << "X: Exit Loop" << endl << endl;

cout << "Please enter your choice: ";
cin >> ch;
ch = toupper(ch);
cout << endl;
} while ((ch != 'A') || (ch != 'S') || (ch != 'M') || (ch != 'D') || (ch != 'X'));

switch (ch) {
case 'A' : f3 = f1 + f2;
cout << "The addition result is: ";
f3.Show();
cout << endl;
cout << "The decimal equivalent is: " << f3.Evaluate();
cout << endl;
break;
case 'S' : f3 = f1 - f2;
cout << "The subtraction result is ";
f3.Show();
cout << endl;
cout << "The decimal equivalent is: " << f3.Evaluate();
cout << endl;
break;
case 'M' : f3 = f1 * f2;
cout << "The multiplication result is: ";
f3.Show();
cout << endl;
cout << "The decimal equivalent is: " << f3.Evaluate();
cout << endl;
break;
case 'D' : f3 = f1 / f2;
cout << "The division result is: ";
f3.Show();
cout << endl;
cout << "The decimal equivalent is: " << f3.Evaluate();
cout << endl;
break;
case 'X' : cout << "Exiting operations loop." << endl;
break;
}
} while (ch != 'X');

cout << "Would you like to enter new fractions? (Y or N) ";
cin >> ch;
cout << endl;
} while (ch == 'Y');

system ("PAUSE");
return 0;
}



Once again, THANK YOU ALL!


Last edited on
It works perfectly fine. In the future, only post in one place. We're active enough we get around to all of the boards to respond to everyone that needs help. Another thing to remember is to use the code formatting tags. They're on the right side of where you post a reply, and they're underneath of where you post a thread, I believe.

As for your code, you only had one issue that I discovered and that was this line:
} while((ch != 'A') || (ch != 'S') || (ch != 'M') || (ch != 'D') || (ch != 'X'));
Should be:
} while((ch != 'A') && (ch != 'S') && (ch != 'M') && (ch != 'D') && (ch != 'X'));

I only tested addition and closed the program once I got the correct result. I hope this helps.
Thank you for your help! Sorry for the late response. I didn't know where the supposed code formatting tags were so thanks for the info.
Topic archived. No new replies allowed.