# 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. ## 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. 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.

3. 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. 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.

5. 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. 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.

7. 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.

8. Yes, quzah is correct- I did not see it fast enough.

9. 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.

10. 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