What is wrong with my Fraction class program?

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!








Hi ! Please use code tags and indentation :)

This looks pretty wierd overall...
1
2
3
4
void print(Fraction f) { //huh?
Fraction();
cout << f.numerator << "/" << f.denominator << "." << endl;
}

Fraction(); probably shouldnt be there ? 'Fraction' is a class. That'd be similar to writting 'int' on that line...

If you're looking to print the numerator and denominator from the Fraction Object you need to create 1 or 2 more member functions whose sole purpose is to return these values. Remember that only ur object has access to its private members. Maybe inline within ur class header like:

GetNumerator() { return numerator; };

then call it like cout << f.GetNumerator();


It's also a little strange to be prompting a value from cin and never using it. Why not just ask for the 2 numbers and just show the '\' on screen or something?
1
2
3
4
void Fraction::Get() { //needs work
char slash;
cin >> numerator >> slash >> denominator;
}
Last edited on
Topic archived. No new replies allowed.