Help with functions

This is a discussion on Help with functions within the C Programming forums, part of the General Programming Boards category; This is the problem I've been assigned and have yet to figure out where I'm going wrong. Write a C ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Help with functions

    This is the problem I've been assigned and have yet to figure out where I'm going wrong.
    Write a C function named liquid() that is to accept an integer number and the addresses of the variables gallons, quarts, pints, and cups. The passed integer represents the total number of cups, and the function is to determine the number of gallons, quarts, pints, and cups in the passed value. Using the passed addresses, the function should directly alter the respective variables in the calling function. Use the relationships of 2 cups to a pint, 4 cups to a quart, and 16 cups to a gallon.

    This is what I have so far but I can't get the correct output, please let me know where i went wrong.

    Code:

    #include <stdio.h>
    void liquid(int ,int*,int*,int*,int*);
    int main()
    {
    int num1=0, cups, pints, quarts, gallons;
    printf("Enter the number of cups:");
    scanf("%2d",&num1);

    liquid(num1, &cups, &pints, &quarts, &gallons);




    return 0;
    }
    void liquid(int x, int *gallons, int *quarts, int *pints, int *cups)
    {

    if (x>=16)

    *gallons=x/16;
    printf("The number of gallons is %d\n",&gallons);

    if (x>=8)

    *quarts=x/8;
    printf("The number of quarts is %d\n",&quarts);

    if (x>=4)

    pints=x/4;
    printf("The number of pints is %d\n",&pints);

    if (x<4)

    *cups=x;
    printf("The number of cups is %d\n",&cups);

    return;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is you aren't calling your funtion correctly. Compare the call:

    liquid(num1, &cups, &pints, &quarts, &gallons);

    With the function definition:

    void liquid(int x, int *gallons, int *quarts, int *pints, int *cups)

    Notice the difference?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    I see the difference but I don't know what to change, everytime I try it I get an error message. Can you give me a hint or point me to an FAQ. I've been stumped for awhile now.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chad03
    I see the difference but I don't know what to change, everytime I try it I get an error message. Can you give me a hint or point me to an FAQ. I've been stumped for awhile now.
    Look again at the two:

    Your problem is you aren't calling your funtion correctly. Compare the call:

    liquid(num1, &cups, &pints, &quarts, &gallons);

    With the function definition:

    void liquid(int x, int *gallons, int *quarts, int *pints, int *cups)

    Your second argument, in the call, you pass it the cups variable. However, in the function itself, you treat that variable as if it were the gallons variable. Simply change the order of the arguments in your function call so they match on both.

    liquid( num1, &gallons ... )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    I'm still getting the same answers 6684040, 6684044, 6684048, 6684052. Is this giving me the addresses? It doesn't change no matter what number I enter.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    My two cents worth:

    Code:
    if (x>=8) 
    
    *quarts=x/8;
    printf("The number of quarts is %d\n",&quarts);
    
    if (x>=4)
    
    pints=x/4;
    printf("The number of pints is %d\n",&pints); 
    
    if (x<4)
    
    *cups=x;
    Should be:
    *pints = x/4;

    Also,
    Code:
    int num1=0, cups, pints, quarts, gallons;
    why are you initializing num 1 to zero here when you are reading it in from the keyboard?

    and yes those are address you are getting.
    Mr. C: Author and Instructor

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chad03
    I'm still getting the same answers 6684040, 6684044, 6684048, 6684052. Is this giving me the addresses? It doesn't change no matter what number I enter.
    Code:
     printf("The number of pints is %d\n",&pints);
    Yep. See the & symbol? That gives you the address of the pointer. You want to dereference it. Use * to dereference it.

    So basicly you had two problems.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Yes, quzah is correct- I did not see it fast enough.
    Mr. C: Author and Instructor

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Mister C
    Yes, quzah is correct- I did not see it fast enough.
    lol. Neither did I. I was just looking at his function calls earlier, and stopped looking beyond that point. Think of me as your compiler's output: Take the first problem and start there. Once that's done, do something else...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    I get the impression the question is asking you to convert 'cups' to all three. eg

    if
    cups = 16
    then
    gallons = 1
    quarts = 4
    pints = 8

    and then print out the value of all three. Therefore you wouldn't need if(x >= 16) etc.

    I would also make gallons, quarts and pints floats, because if cups was 3 then pints should be 1.5 etc.

    You don't need 'x', use 'cups' when asking for input.
    Your calculations should be:

    *gallons=x/16;

    *quarts=x/4;

    *pints=x/2;

    Hope I've got this right.

    hobo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21