Good Afternoon,

I'm having problems with my program that deals with fractions. The problems are with the functions multiply and divide that are not working correctly. So far I have this code.

CPP FILE:

#include <iostream>
#include <cmath>

#include "hw4_head.h"
using namespace std;

int main ()
{
    int a, b, c, d, e, f, gcd;
    int option;
    char op;
    char answer = 'y';
    while (answer != 'n')
    {
    showMenu();

    cout <<"Enter a choice ";
    cin>>option;

    switch(option)
    {
    case 1:
            cout <<"Going to perform the ADD operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Add (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 2:
            cout <<"Going to perform the SUBSTRACT operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Substract (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 3:
            cout <<"Going to perform the DIVIDE operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Divide (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    case 4:
            cout <<"Going to perform the MULTIPLY operation on a fraction question"<<endl;
            cout << "Please enter a fraction question \n"
                 <<"in the form of a / b operation c / d ";
            getFract (a, b);
            op = getOperator ();
            getFract (c, d);

            disPlayFract (a, b);
            cout << " " << op << " ";
            disPlayFract (a, d);
            cout <<endl;

            Multiply (a, b, c, d, e, f);
            cout<<"The answer is equal to"<<endl;
            disPlayFract (e, f);
            cout <<endl;

            gcd = gcf (e, f);
            e = e / gcd;
            f = f / gcd;
            cout <<"The answer in reduce form is"<<endl;
            disPlayFract (e, f);
            cout<<endl;
            break;
    default:
            cout <<"You entered an invalid input"<<endl;
    }

    cout << "Would you like to workout another problem (y/n) ";
    cin>>answer;

    }
    cout <<"Good Bye."<<endl;


    return 0;
}

Header file:

#include <iostream>
#include <cmath>

using namespace std;

#ifndef     HW4_HEAD_H
#define     HW4_HEAD_H

void getFract( int& a, int& b)
{
    char dummy;
    cin>>a>>dummy>>b;
}

char getOperator ()
{
    char op;
    cin>>op;
    return op;
}

void disPlayFract (const int a, const int b)
{
    cout << a << " / "<< b;
}

void Add (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * d + b * c;
    f = b * d;
}

void Substract (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * d - b * c;
    f = b * d;
}

void Divide (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = (a * d) / (c *b);
    f = c * d;
}

void Multiply (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = (a / d) * (b / c);
    f = b * d;
}
int gcf (int A, int B)
{
    int R;
    R = A % B;
    while (R != 0)
    {
        A = B;
        B = R;
        R = A % B;
    }
    return B;
}

void showMenu()
{
    cout<<"**************Fractional Arithmetic Program Menu***********************"<<endl;
    cout<<"Enter 1 to perform ADD operation on a fraction question"<<endl;
    cout<<"Enter 2 to perform SUBSTRACT operation on a fraction question"<<endl;
    cout<<"Enter 3 to perform DIVIDE operation on a fraction question"<<endl;
    cout<<"Enter 4 to perform MULTIPLY operation on a fraction question"<<endl;
}

#endif

I really appreciate any help that I can get.

jaw drops

You'll need help if this is the way you're being taught how to do things. You don't even know how much you need it.

If you don't mind me asking, has your coursework covered basic data structures - using either struct or class - yet, and if so, did the instructor show you how they would be applied to this project? If so, then you've misunderstood the project; if not, then the instructor has misunderstood it, which is even worse. Given what I've seen so far of the way your course is being taught, I can hazard a guess, but I'd prefer to give the professor the benefit of a doubt.

OK, personal revulsion at the particular way this is being implemented aside, let's fix the division and multiplication methods. Right now, your formulae are entirely wrong for these two functions, and I'm not sure just how you (or, I'm guessing, your professor) managed to come up with the approach given. The correct formula for division is

//  e/f = (a/b) / (e/f) = (b * c) / (a * d) 

void Divide (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = b * c;   
    f = a * d;
}

and

// e/f = (a/b) * (c/d) = (a * c) / (b * d)
void Multiply (const int a, const int b, const int c, const int d, int& e, int& f)
{
    e = a * c;
    f = b * d;
}

Except, of course, that no experienced programmer in his or her right mind would do it this way. The whole point of assigning a fractions project is to teach the students how to use compound data structures; it is both uniformative and confusing to do this exercise otherwise, unless the intention is to have the students re-do the whole thing later using a struct type or a class. Even if that is what the professor has in mind, it's still a rather wasteful way to do this.

Sorry if I'm ranting again, but this sort of incompetent teaching is inexcusable.

// e/f = (a/b) / (e/f) = (b * c) / (a * d)

e/f = (a/b) / (c/d) = (a/b) * (d/c) = (a * d) / (b * c), you mean?

commented: Oopps, thanks for the correction. +8

Ugh, you are correct, I got it backwards. Sorry if I misled you PoloBlue, especially given my ranting about careless instructions.

Schol-Rea-Lea and pyTony,

Thank you for your help, I really appreciated your input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.