tags:

views:

100

answers:

2
Fraction AddFractions(const Fraction& frac1, const Fraction& frac2)
{
    Fraction frac3;

    if (frac1.denom == frac2.denom)
    {
        frac3.num = frac1.num + frac2.num;
        frac3.denom = frac1.denom;          
    }
    else
    {
        frac3.denom = (frac1.denom * frac2.denom);
        frac3.num = (frac1.num * frac2.denom) + (frac2.num * frac1.denom);          
    }
    return frac3;
}

This is my prototype and there is a structure defined as Fraction with int num and int denom when I call the function it just gives me back garbage? I don't know what's wrong with it. Please help. =(

    cout << "Please enter two Fractions" << endl;
    cout << endl;
    GetFraction(frac1); 
    cout << "Your fisrt fraction is: ";
    PrintFraction(frac1);
    cout << endl;
    cout << endl;
    GetFraction(frac2);
    cout << "Your second fraction is: ";
    PrintFraction(frac2);
    cout << endl;


    do
    {
        choice1 = MathMenu();
        if (choice1 == ErrorOpt)
        {
            cout << "There was an error with what you have entered" << endl;
        }
        else if(choice1 != QuitOpt)
        {

            // do the math options 
            switch (choice1)
            {
            case AddOpt:
                AddFractions(frac1, frac2);
                break;
            case SubtractOpt:
                SubtractFractions(frac1, frac2);
                break;
            case MultiplyOpt:
                MultiplyFractions(frac1, frac2);
                break;
            case DivideOpt:
                DivideFractions(frac1, frac2);
                break;
            }
            //print the results 
            PrintFraction(frac1);
            PrintMathOption(choice1);
            PrintFraction(frac2);
            cout << " = ";
            Standardize(frac3);
            Reduce(frac3);
            PrintFraction(frac3);
            cout << endl;
            Pause();
+3  A: 

You should assign frac3 e.g.:

frac3 = AddFractions(frac1, frac2);
J.F. Sebastian
@Potatoswatter: the result of the function can be safely ignored, but printing an uninitialized variable may produce garbage.
J.F. Sebastian
Sorry, I didn't see that he had a variable named `frac3` at all. Never mind.
Potatoswatter
+2  A: 

From what I can tell from your code; there is no variable frac3 in the scope of:

        Standardize(frac3);
        Reduce(frac3);
        PrintFraction(frac3);

And your math functions are being called but you are not capturing the returns.

Duracell