-1

In my assignment, I have been stuck on one area know matter how many times I read the text book or try and search for help. I feel really confused in this spot, and could really use some advice so I can get over this hurdle as well as learn what is proper. I am still new to programming. So bear with me. We have two .cpp files and one .h file. One of the .cpp has a switch statement that calls different functions depending on users choice. In the old assignment, it was just calling functions, but we are using the same assignment and changing it to overloaded operators. We are instructed to manipulate the .cpp file to replace function calls with our operators. But, I can not figure out how to call those operators, so that they print out their prompts and take in the users values through the operators rather than the old functions. TIA for any help.

.h file

    #ifndef _FRACTION_H_
    #define _FRACTION_H_
    #include "iostream"
    using namespace std;

    class fraction
    {
    private:
        int      numerator;
        int      denominator;

    public:
        fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {};
        friend fraction operator+(fraction f1, fraction f2);
        friend fraction operator-(fraction f1, fraction f2);
        friend fraction operator*(fraction f1, fraction f2);
        friend fraction operator/(fraction f1, fraction f2);
        friend ostream& operator<<(ostream& out, fraction& f);
        friend istream& operator>>(istream& in, fraction& f);
    };

    #endif

.cpp file

#include <iostream>
#include "fraction.h"
using namespace std;


int gcd(int u, int v);



fraction::fraction(int n, int d) : numerator(n), denominator(d)
{
    int     common = gcd(numerator, denominator);
    numerator /= common;
    denominator /= common;
}



fraction operator+(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}



fraction operator-(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}


fraction operator*(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.numerator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}


fraction operator/(fraction f1, fraction f2)
{
    int     n = f1.numerator * f2.denominator;
    int     d = f1.denominator * f2.numerator;

    return fraction(n, d);
}


ostream& operator<<(ostream& out, fraction& f)
{
    out << f.numerator << "/" << f.denominator << endl;

    return out;
}



istream& operator>>(istream& in, fraction& f)
{
    cout << "Please enter the numerator: ";
    cin >> f.numerator;
    cout << "Please enter the denominator: ";
    cin >> f.denominator;

    return in;
}



// Euclid's Algorithm for finding the greatest common divisor

int gcd(int u, int v)
{
    u = (u < 0) ? -u : u;
    v = (v < 0) ? -v : v;

    while (u > 0)
    {
        if (u < v)
        {
            int t = u;  // swap u and v
            u = v;
            v = t;
        }

        u -= v;
    }

    return v;           // the GCD of u and v
}

.cpp file (this is the one I need to replace function calls with operators) This is where I am stuck, and not sure how to replace my function calls with my operators.

#include "fraction.h"
#include <iostream>
using namespace std;

int main()
{
    char        choice;
    fraction    left;
    fraction    right;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
            case 'A':
            case 'a':
                cout << "Adding" << endl;
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.add(right).print();
                break;
            case 'S':
            case 's':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.sub(right).print();
                break;
            case 'M':
            case 'm':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.mult(right).print();
                break;
            case 'D':
            case 'd':
                cout << "enter the first operand: ";
                left.read();
                cout << "enter the second operand: ";
                right.read();
                left.div(right).print();
                break;
            case 'E' :
            case 'e' :
                break;
            default:
                cerr << "Unrecognized choice: " << choice << endl;
                break;
        }
    } while (choice != 'e' && choice != 'E');

    return 0;
}

1 Answers1

0

Change calls to read() into uses of cin >>, print() is replaced with cout <<, and methods like add() get replaced with their corresponding operators.

case 'A':
case 'a':
    cout << "Adding" << endl << "enter the first operand: ";
    cin >> left;
    cout << "enter the second operand: ";
    cin >> right;
    cout << left + right;
    break;

You also should change the parameter types of all the operators except >> to const fraction &. This solves the errors about not finding a matching function.

Here's the full code (I only included the A operation, the rest are similar):

#include "iostream"
using namespace std;

class fraction
{
private:
    int      numerator;
    int      denominator;

public:
    fraction(int n = 0, int d = 1);
    friend fraction operator+(const fraction &f1, const fraction &f2);
    friend fraction operator-(const fraction &f1, const fraction &f2);
    friend fraction operator*(const fraction &f1, const fraction &f2);
    friend fraction operator/(const fraction &f1, const fraction &f2);
    friend ostream& operator<<(ostream& out, const fraction& f);
    friend istream& operator>>(istream& in, fraction& f);
};

using namespace std;


int gcd(int u, int v);

fraction::fraction(int n, int d) : numerator(n), denominator(d)
{
    int     common = gcd(numerator, denominator);
    numerator /= common;
    denominator /= common;
}

fraction operator+(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator-(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator*(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.numerator;
    int     d = f1.denominator * f2.denominator;

    return fraction(n, d);
}

fraction operator/(const fraction &f1, const fraction &f2)
{
    int     n = f1.numerator * f2.denominator;
    int     d = f1.denominator * f2.numerator;

    return fraction(n, d);
}

ostream& operator<<(ostream& out, const fraction& f)
{
    out << f.numerator << "/" << f.denominator << endl;

    return out;
}



istream& operator>>(istream& in, fraction& f)
{
    cout << "Please enter the numerator: ";
    cin >> f.numerator;
    cout << "Please enter the denominator: ";
    cin >> f.denominator;

    return in;
}



// Euclid's Algorithm for finding the greatest common divisor

int gcd(int u, int v)
{
    u = (u < 0) ? -u : u;
    v = (v < 0) ? -v : v;

    while (u > 0)
    {
        if (u < v)
        {
            int t = u;  // swap u and v
            u = v;
            v = t;
        }

        u -= v;
    }

    return v;           // the GCD of u and v
}

int main()
{
    char        choice;
    fraction    left;
    fraction    right;

    do
    {
        cout << "A\tAdd\n";
        cout << "S\tSub\n";
        cout << "M\tMult\n";
        cout << "D\tDiv\n";
        cout << "E\tExit\n";

        cout << "\nChoice?: ";

        cin >> choice;
        cin.ignore();

        switch (choice)
        {
        case 'A':
        case 'a':
            cout << "Adding" << endl << "enter the first operand: ";
            cin >> left;
            cout << "enter the second operand: ";
            cin >> right;
            cout << left + right;
            break;
        case 'E' :
        case 'e' :
            break;
        default:
            cerr << "Unrecognized choice: " << choice << endl;
            break;
        }
    } while (choice != 'e' && choice != 'E');

    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • when i use the cout << left + right, it tells me that << does not have any matching operators – Jessen Gilbert Apr 04 '18 at 23:27
  • `left` and `right` are declared as `fraction`. There is an overloaded `operator<<` defined to print out a `fraction`, so it should work. On a side note, all of the `friend` operators should be taking `fraction` values as `const fraction &`, except for `operator>>` – Remy Lebeau Apr 04 '18 at 23:37
  • I'm actually just getting a ton of errors in my code now since switching to this. It wouldnt accept just doing cout <<. I'm honestly just confused as to how all this works. In my operators I have them printing to the page, so wouldn't I just call them so that they run at that time and print to the console? Also, how does it know that cin >> left; is actually going to the istream& operator? – Jessen Gilbert Apr 04 '18 at 23:53
  • It knows it because it looks for an overload of the `>>` operator that has arguments matching `istream` and `fraction`. – Barmar Apr 04 '18 at 23:55
  • Ok, I got the cout << to work by creating more fraction objects, and saving the operator into it before doing a cout << and it took that error away. When I run it, it asks for the first operand, and after inputting the number, it doesn't jump to the next prompt, it wants two numbers put in rather than just the numerator. – Jessen Gilbert Apr 05 '18 at 00:06
  • Try flushing `cout` after the second prompt. – Barmar Apr 05 '18 at 00:09
  • See https://stackoverflow.com/questions/2704752/flushing-of-cout-prior-to-reading-input-using-cin-why/2704757 – Barmar Apr 05 '18 at 00:09
  • My values aren't getting to the right function or something. All my answers come out as 0/1 – Jessen Gilbert Apr 05 '18 at 00:33
  • I can't reproduce that. Adding all the `const fraction &` types to the function parameters got rid of all my errors, and when I add `3/4` and `1/2` I get `5/4`. – Barmar Apr 05 '18 at 00:43
  • I think the const was screwing me up, because once i did it, it worked fine. I was putting it in the wrong spot until I saw your code. Now it makes sense as to what was going wrong. Thank you so much for your help. This got it working for me! – Jessen Gilbert Apr 05 '18 at 02:11