Need help adding and reducing fractions

#include"hw3functions.hpp"
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

int main()
{
char pick = ' ';//variable that determines user choice for addition or subtraction
char keepGoing;//variable that determines to continue or quit
do
{
while (pick != 'Q')
{
system("cls");//clear console
showMenu(pick);//to menu
switch (toupper(pick))//reinitializes value in upper case
{
case 'A':
add();
break;
case 'S':
subtract();
break;
case 'Q':
system("pause");
return 0;
break;
default:
break;
}
}

cout << "\nDo you want to do more addtions? (Y/N): ";
cin >> keepGoing;//used to determines if user wants to continue with program

keepGoing = toupper(keepGoing);//reinitializes value in upper case

} while (keepGoing == 'Y');

system("pause");

return 0;
}

#*********FUNCTIONS**************
#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

//Prototypes
void GetRational(int& num, int& den);
void AddRational(int& anum, int& aden, int num1, int den1, int num2, int den2);
void SubtractRational(int& anum, int& aden, int num1, int den1, int num2, int den2);
void reduce(int& num, int& den);
void DisplayRational(int num, int den);

//Displays Menu
void showMenu(char& pick)
{
cout << "Rational numbers calculator\n";
cout << "\n(A)ddition\n";
cout << "(S)ubtraction\n";
cout << "(Q)uit\n";
cout << "\nEnter your option: ";
cin >> pick;//stores user's pick

return;
}

//Used when user picks Addition
void add()
{
int num1, den1, num2, den2, anum, aden;//variables used
char goingAdd;//variable used to continue addition

do {
system("cls");//clear console
cout << "Addition of rational numbers\n";//title

GetRational(num1, den1);//calls function to get 1st fraction
GetRational(num2, den2);//calls function to get 2nd fraction

AddRational(anum, aden, num1, den1, num2, den2);//calls function to process addition

cout << "\nThe result of " << num1 << "/" << den1 << " + " << num2 << "/" << den2 << " = ";

DisplayRational(anum, aden);//displays result

cout << "\nDo you want to do more additions? (Y/N): ";
cin >> goingAdd;//stores user's choice
goingAdd = toupper(goingAdd);//makes choice uppercase

} while (goingAdd == 'Y');

return;
}

//Used when user picks Subtraction
void subtract()
{
int num1, den1, num2, den2, anum, aden;//varibales used
char goingAdd;//variable used to continue subtraction

do {
system("cls");//clears console
cout << "Subtraction of rational numbers\n";//title

GetRational(num1, den1);//calls function to get 1st fraction
GetRational(num2, den2);//calls function to get 2nd fraction

SubtractRational(anum, aden, num1, den1, num2, den2);//calls function to process subtraction

cout << "\nThe result of " << num1 << "/" << den1 << " - " << num2 << "/" << den2 << " = ";

DisplayRational(anum, aden);//display result

cout << "\nDo you want to do more subtractions? (Y/N): ";
cin >> goingAdd;//stores user's choice
goingAdd = toupper(goingAdd);//makes choice uppercase

} while (goingAdd == 'Y');

return;
}

// This function shows the prompt Please enter a fraction (n/d): , gets the values
// from the keyboard and returns them to the caller. Since the division operator (/)
// is read in along with the numbers the function must read and discard it.
// This function must reject a denominator equal to zero.
void GetRational(int& num, int& den)
{
string top, bot;//used to hold numerator and denominator

cout << "\nPlease enter a fraction (n/d): ";

getline(cin, top, '/');//used to store everything before '/' or everything in line,
cin >> bot;//used to store info after '/'

num = stoi(top);//string to integer and stores in variable num

den = stoi(bot);//string to integer and stores in variable den

while (den == 0)//determines if zero in denominator and asks again for fraction
{
cout << "\nSorry, division by zero is not allowed!\n\n";
system("pause");

cout << "\nPlease enter a fraction (n/d): ";

getline(cin, top, '/');
cin >> bot;

den = stoi(bot);
}

return;
}

// num1, den1, num2, and den2 are provided by the caller.
// anum and aden are calculated using the formula shown above and returned to the
// caller after they are reduced. To reduce the fraction this function must call
// the function reduce(anum, aden).
void AddRational(int& anum, int& aden, int num1, int den1, int num2, int den2)
{
anum = (num1 * den2) + (num2 * den1);//used to get 1st and 2nd fraction sum
aden = (den1 * den2);

reduce(anum, aden);//calls function to reduce sum of 1st and 2nd fraction

return;
}

// num1, den1, num2, and den2 are provided by the caller.
// anum and aden are calculated using the formula shown above and returned to the
// caller after they are reduced. To reduce the fraction this function must call
// the function reduce(anum, aden).
void SubtractRational(int& anum, int& aden, int num1, int den1, int num2, int den2)
{
anum = (num1 * den2) - (num2 * den1);//used to get 1st and 2nd fraction difference
aden = (den1 * den2);

reduce(anum, aden);// calls function to reduce difference of 1st and 2nd fraction

return;
}

// num and den are provided by the caller.
// num and den are reduced according to the algorithm explained below and returned to
// the caller.
void reduce(int& num, int& den)
{
// num and den are provided by the caller.
// num and den are reduced according to the algorithm explained below and returned to
// the caller.
int remain;
int gcd;
remain = num%den;
while(remain != 0)
{
num = den;
den = remain;
remain = num%den;
}
gcd = den;
num /= gcd;
den /= gcd;

}

// num and den are provided by the caller.
// The numbers are shown in the form num/den unless den is equal to 1, in this case
// only the numerator should be displayed.
void DisplayRational(int num, int den)
{

if (den == 1)//catches if den is equal to 1
{
cout << num << endl;
}
else if (num == 0)//displays 0 if num equals 0
{
cout << num << endl;
}
else
{
cout << num << "/" << den << endl;//if above exceptions are not found program displays
}

return;
}








What is the actual, specific problem?

Also, please edit your post and put [code] {code here} [/code] tags surrounding your code.

1
2
3
gcd = den;
num /= gcd;
den /= gcd;

One problem is that you assign the value of den to gcd, and then you divide den by gcd. In other words, you're dividing den by den. This will always make den be 1, even if the fraction isn't reducible to a whole number.

I would write a separate function for finding the gcd of two numbers, that is then called from reduce(), to clarify the logic of what's happening there.
Last edited on
std::gcd ()


Topic archived. No new replies allowed.