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?
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.
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;
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;
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');
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?