#Simple multiplying fractions program (C++)

11 messages · Page 1 of 1 (latest)

drifting mantle
#

Im a Nuub at c-- from learncpp, code works but idk if i've used the best practices and feel like ive just janked this code to make it work. Basically please see if this code is written efficiently. Theres some commented code when i was trying to make it work i found that the takeFraction() was the problem. If anyone can explain why.

#

ignore the stuff about revenue

#
struct Fraction {
    int numerator{};    // numerator member for Fraction
    int denominator{};    // denominator member for Fraction
};

/*
Fraction takeFraction(const Fraction& fraction)    // Function to take in user fraction
{
    int num_input{};    // numerator input
    int den_input{};    // denominator input
    std::cout << "Enter a value for the numerator: ";
    std::cin >> num_input;
    num_input = fraction.numerator;    // numerator assigned to fraction.numerator
    std::cout << "Enter a value for the denominator: ";
    std::cin >> den_input;
    den_input = fraction.denominator;    // denominator assigned to fraction.denominator

    std::cout << "Entered: " << fraction.numerator << "/" << fraction.denominator << std::endl;


    return fraction;    // return the fraction object
}
*/

Fraction takeFraction()
{
    Fraction fraction{};
    std::cout << "Enter a value for the numerator: ";
    std::cin >> fraction.numerator;
    std::cout << "Enter a value for the denominator: ";
    std::cin >> fraction.denominator;

    std::cout << "Entered: " << fraction.numerator << "/" << fraction.denominator << std::endl;

    return fraction;

}

Fraction multiplyFractions(const Fraction& mem1, const Fraction& mem2)    // function to multiply two fractions
{
    Fraction temp;    // tempporary fraction object
    temp.numerator = mem1.numerator * mem2.numerator;    // mulitply first and second fraction's numerators
    temp.denominator = mem1.denominator * mem2.denominator;
    
    return temp;
}

void printFraction(const Fraction& result)
{
    std::cout << "Your fraction multiplied together: " << result.numerator << "/" << result.denominator;
}


int main() {

    /*
    Fraction test1{};
    Fraction test2{};
    takeFraction(test1);
    takeFraction(test2);
    Fraction result{ multiplyFractions(test1, test2) };
    printFraction(result);
    */
    //printFraction(multiplyFractions(takeFraction(test1), takeFraction(test2)));
    //Fraction result{ multiplyFractions(takeFraction(test1), takeFraction(test2)) };
    //printFraction(result);

    Fraction result{ multiplyFractions(takeFraction(), takeFraction()) };
    printFraction(result);


    return 0;
}```
lofty hatch
#

As a fellow beginner, I cant really give you feedback on the actual code but I would recommend that instead of inputting the numerator and denominator separately, you should just be able to input 1/2 for example

#

Just a qol thing

robust locust
#

program does infact multiply fractions

#

challenge: add functionality that simplifies the result fraction

keen dock
lofty hatch
#

Also i would put the multiply function inside the struct

lofty hatch
#

As well as the other mathematical operations if you add them