Thread: trouble with functions with &

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    trouble with functions with &

    this is suppose to add two functions

    void AddFractions(int & x, int & y, int & x2, int & y2);
    void GetFrac(int, int);

    case 1: GetFrac(x, y);
    GetFrac(x2, y2);
    AddFractions(int & x, int & y, int & x2, int & y2);
    cout << "The sum of " << x << " / " << y << " + " << x2;
    cout << " / " << y2 << " = " << sum << endl;
    break;
    void GetFrac(int & num, int & denom)
    {
    char slash;
    int n, d;
    cin >> n >> slash >> d;

    }


    void AddFractions(int & x, int & y, int & x2, int & y2)
    {
    int denom1, n, d, a, b, c, e, sum = 0;
    denom1 = c * e;
    n = (a/c) * denom1;
    d = (b/e) * denom1;
    sum = n + d;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    15

    Unhappy

    Sorry, but I'm very confused...
    I'm assuming the case 1: is part of a larger switch statement you've ommited, and that you've declared x, y, x2, and y2 before the code segment you've shown me, since you use them in calls to GetFrac(). Based on these assumptions, there are several things I don't understand...
    1. Why are you redeclaring x, y, x2, and y2 in your call to AddFractions? Also, if you're passing parameters by reference, you don't need to include the ampersand in your function call. In other words, change your call to
    AddFractions(x, y, x2, y2);

    2. Your use of the variable sum confused me. Nothing is passed back from AddFractions(), so I would assume sum was a global, but you declare as auto inside the definition of AddFractions(). Either declare sum as global or have AffFractions() return the sum.

    3. You don't use the variables x, y, x2, or y2 at all in your AddFractions() function. Replace a with x, c with y, b with x2, and e with y2.

    3. You declared all your variables in AddFractions() as ints, which means the division is going to be doing a lot of truncating that is probably undesirable.

    Here, let me just rewrite that function for you:

    int AddFractions(int &num1, int &den1, int &num2, int &den2)
    {
    return (int)( (float)(num1*den2+num2*den1)/(float)(den1*den2));
    }

    Note that this will still only return an integer, not a float.
    Sorry about the messy casts; you could probably rewrite it in pieces if you wanted to to make it more comprehensible.

    4. GetFract() also needs a bit of work. Since you're passing by num and den by reference, you don't have to return anything; you do, however, have to assign something to them! Erase the line
    int n,d;
    and replace the n and d in the cin line with num and den.
    Last edited by DarkDragon; 12-24-2001 at 10:54 PM.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    im getting there

    i have 2 parse errors and "variables shadow parameters"
    can u give me a little more help

    int DisplayMenu(); //display menu / input choice
    void GetFrac(int, int); //inputs one fraction that the user enters in form num/denom
    void DisplayFrac(int, int); //outputs the fraction in form num/denom
    void ReduceFrac(int, int); //reduces a fraction to lowest terms
    void AddFractions(int & x, int & y, int & x2, int & y2);
    void SubtractFractions(int & x, int & y, int & x2, int & y2);
    void MultiplyFractions();
    void DivideFractions();
    double ConvertToDecimal(int, int);

    int main()
    {
    int choice, x, y, x2, y2, sum;
    choice = 1;

    while(choice != 7)
    {
    DisplayMenu();
    switch(choice)
    {
    case 1: GetFrac(x, y);
    GetFrac(x2, y2);
    AddFractions(int x, int y, int x2, int y2);
    cout << "The sum of " << x << " / " << y << " + " << x2;
    cout << " / " << y2 << " = " << sum << endl;
    break;
    case 2: GetFrac(x, y);
    GetFrac(x2, y2);
    SubtractFractions(int x, int y, int x2, int y2);
    cout <<"The sum of " << x << " / " << y << " + " << x2;
    cout << " / " << y2 << " = " << sum << endl;
    break;
    case 3: GetFrac(x, y);
    GetFrac(x2, y2);

    break;
    case 4: GetFrac(x, y);
    GetFrac(x2, y2);

    break;
    case 5: GetFrac(x, y);
    break;
    case 6: GetFrac(x, y);
    ConvertToDecimal(x, y);
    break;
    case 7: cout <<"Thank you for using this program" << endl;
    break;
    }
    }
    return 0;
    }

    int DisplayMenu()
    {
    int choice;
    cout <<" Menu" << endl;
    cout <<"1. Add two fractions" << endl;
    cout <<"2. Subtract two fractions" << endl;
    cout <<"3. Multipy two fractions" << endl;
    cout <<"4. Divide one fraction by another" << endl;
    cout <<"5. Reduce a fraction to lowest terms" << endl;
    cout <<"6. Convert a fraction to a decimal" << endl;
    cout <<"7. Quit" << endl;
    cout <<"Please choose from the following menu --> ";
    cin >> choice;
    cout << endl;
    return choice;
    }

    void GetFrac(int & num, int & denom)
    {
    char slash;
    cin >> num >> slash >> denom;

    }
    void DisplayFrac(int num, int denom)
    {
    int n, d;
    cout << n <<" / " << d << endl;
    }
    void ReduceFrac(int num, int denom)
    {

    }
    void AddFractions(int & x, int & y, int & x2, int & y2)
    {
    int denom1, n, d, x, y, x2, y2, sum = 0;
    denom1 = y * y2;
    n = (x/y) * denom1;
    d = (x2/y2) * denom1;
    sum = n + d;
    return sum;
    }
    void SubtractFractions(int & x, int & y, int & x2, int & y2)
    {
    int denom1, n, d, x, x2, y, y2, sum = 0;
    denom1 = y * y2;
    n = (x/y) * denom1;
    d = (x2/y2) * denom1;
    sum = n - d;
    return sum;
    }
    void MultiplyFractions()
    {
    int nume, denom, x, y, x2, y2;
    nume = x * x2;
    denom = y * y2;

    }
    void DivideFractions()
    {
    int x, y, x2, y2, nume, denom;
    nume = x * y2;
    denom = y * x2;
    }
    double ConvertToDecimal(int num, int denom)
    {
    int sum;
    sum = num/denom;
    return sum;
    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    15
    Sure.
    First, don't forget to #include <iostream.h> at the beginning of your program if you want to use cout/cin.
    Second, lose the data type keywords when calling functions. That is, write
    AddFractions(x, y, x2, y2);
    not
    AddFractions(int x, int y, int x2, int y2);

    In AddFractions(), you've already defined x, y, x2, and y2 in the parameter list, so you don't need (and can't) redefine them later in the function. In other words, change
    int denom1, n, d, x, y, x2, y2, sum = 0;
    to
    int denom1, n, d, sum = 0;


    Also, AddFractions() still has truncation problems since you're performing division on ints. See above for a possible (albeit messy) solution to this.

    If you're going to return something from AddFractions(), you have to specify the return type in your prototype and function definition. Since you're returning an int, change void to int.

    Most of the above corrections also apply to the function SubtractFractions().

    Lastly, you have a plethora of scope/uninitialized variable problems. Find a good C/C++ book/tutorial and read about variable scope, then look at your program and see if you can figure out and fix what you're doing wrong. If not, come back and I'll try to sort it all out.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    thanks

    i appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM