this is a c question need some help

okay i need to be able to tell by a user defined amount if/and how many it can go into 3 other fields with the reminder also being displayed all with in main. It has to pass mulitpul variables with in the function back to main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//3/4/08
//WA 3 section B question 2
//

#include <stdio.h>
#include <stdlib.h>





int main ()

{
	char UsrInput[255];
	int gallons = 0;
	int quarts = 0;
	int pints = 0;
	
	int cups = 0;
	
	printf("please enter the a how many cups you have so it can be converted \n");
	fgets(UsrInput, 255, stdin);
	cups = atoi (UsrInput);
	
        //this is the function call with the variables needing to be 
        //changed also in it.
	liquid (gallons, quarts, pints, cups);
	
       // this is the part that is going to display the end result has to 
       // be here
	printf("gallons %d. \nquarts %d. \n", gallons, quarts);
	printf("pints %d. \ncups %d . \n", pints, cups);
	
	system ("pause");
	return 0;	
	
}

//liquid declaring the other variable results that will be passed back
int liquid (int g, int q, int p, int c)

{


	
	
	/*doing increments becuase everytime it subtracts the total number of cups it adds one to the 
	 total number of gallons, quarts, and pints, and also takes away from cups till the reminder is
	 passed back to the cups portion of the program to be displayed */
	

       while (c > 15) {
		++ g;
		c = c - 16;
		
	} 
	
	while (c > 3) {
		++ q;
		c = c - 4;
		
	}

	while (c > 1) {
		++ p;
		c = c - 2;
		
	}
 
	
	//the return of the variables that should have the information in them
	return g, q, p, c;
	
} 


I know this should be easy but for some reason im getting stumped the increments and all the math is sound cause it worked when it was with in the function but its just not wanting to pass the variables back up to the top. And i dont know if i have included address or not. so please help.
Last edited on
I found the answer here [url]http://irc.essex.ac.uk/www.iota-six.co.uk/c/f5_using_pointers.asp[/url] i need to use pointers and addressing to pass the functions between the two. It also helps if you state the function stuff before main so when you do call the function its already defind it helps with some compliers ive heard. But the arrays work greart and get the job done if any one can tell me excatly why it does this i would greatly appreciate it. Becuase the only way i can see it is the & operator acts like and achore that the * opertor points to. Is the only thing i could come up with. Thanks for any advice and help in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>

//insure that cups is passed to liquid by putting the variable inside of liquid.
void liquid (int *ga, int *qu, int *pi, int *cu);


int main ()

{
	char UsrInput[255];
	int g = 0;
	int q = 0;
	int p = 0;
	int c = 0;
	int *gallons = &g;
	int *quarts = &q;
	int *pints = &p;
	int *cups= &c;
/* the ones with the '&' operator have to be decleared with a number before they can be used for what you want. The you attach the to the *
and they point to each other. */
	
	
	printf("please enter the a how many cups you have so it can be converted \n");
	scanf("%d", &cups[c]);
	
	liquid (&g, &q, &p, &c);
	
	printf("gallons %d. \nquarts %d. \n", g, q);
	printf("pints %d. \ncups %d . \n", p, c);
	
	system ("pause");
	return 0;	
	
}
// i took the first two letters from the words above to make function 
//specific variables so the function could work with its own addressing. 
// and allow the function call atop to send the other variables and
//assign them to the function specific one. thats why i kept the same
//variables in the same spot.

void liquid (int *ga, int *qu, int *pi, int *cu)

{


	
	
	/*doing increments becuase everytime it subtracts the total number of cups it adds one to the 
	 total number of gallons, quarts, and pints, and also takes away from cups till the reminder is
	 passed back to main() to be displayed */
	
	while (*cu > 15) {
		++ *ga;
		*cu = *cu - 16;
		
	} 
	
	while (*cu > 3) {
		++ *qu;
		*cu = *cu - 4;
		
	}

	while (*cu > 1) {
		++ *pi;
		*cu = *cu - 2;
		
	}
 
	

	
} 
Last edited on
Topic archived. No new replies allowed.