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