![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 16
| Need help with liquid measurements using functions and remainders. Code: #include <stdio.h>
void liquid(float*, float*, float*, float*);
int main()
{
int remainder, input;
float gallons, quarts, pints, cups;
printf("How many cups do you have?: \n");
scanf("%f",&input);
gallons = input/16;
remainder = input % 16;
quarts = remainder/4;
remainder = remainder % 4;
pints = remainder/2;
remainder = remainder % 2;
cups = remainder;
liquid(&gallons, &quarts, &pints, &cups);
return 0;
}
void liquid(float *gallons, float *quarts, float *pints, float *cups)
{
printf("You have %f gallon(s).\n",*gallons);
printf("You have %f quart(s).\n",*quarts);
printf("You have %f pint(s).\n",*pints);
printf("You have %f cup(s).\n",*cups);
return;
}
|
| RenFromPenn is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You can't use %f to read into an integer variable. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Nov 2008
Posts: 16
| I tried using %d and it displayed nothing but zeros. Please help me fix this. |
| RenFromPenn is offline | |
| | #4 |
| Banned Join Date: Dec 2008 Location: Maputo, Mozambique
Posts: 82
| Code: #include <stdio.h>
void liquid(float, float, float, float);
int main(void)
{
int remainder, input;
float gallons, quarts, pints, cups;
printf("How many cups do you have?: \n");
scanf("%d",&input);
gallons = (float)input/16.0f;
remainder = input % 16;
quarts = (float)remainder/4.0f;
remainder = remainder % 4;
pints = (float)remainder/2.0f;
remainder = remainder % 2;
cups = remainder;
liquid(&gallons, &quarts, &pints, &cups);
return 0;
}
void liquid(float gallons, float quarts, float pints, float cups)
{
printf("You have %f gallon(s).\n",gallons);
printf("You have %f quart(s).\n",quarts);
printf("You have %f pint(s).\n",pints);
printf("You have %f cup(s).\n",cups);
return;
}
|
| c++0x is offline | |
| | #5 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Code: $ ./temp How many cups do you have?: 19 You have 1.000000 gallon(s). You have 0.000000 quart(s). You have 1.000000 pint(s). You have 1.000000 cup(s). | |
| tabstop is offline | |
| | #6 |
| Registered User Join Date: Nov 2008
Posts: 16
| When I ran the program, my results were nothing but zeros. Are you telling me that when you ran the program and only changed the %f to %d that it worked? |
| RenFromPenn is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| That is correct. (Note that you only change the %d where the variable is an integer -- so just in the one place.) |
| tabstop is offline | |
| | #8 |
| Registered User Join Date: Nov 2008
Posts: 16
| So, I just change it for scanf("%d",&input); Right? That would explain why I go all zeroes. I changed it to %d in every instance. Oh, and how to I get it to display just the number 1 instead of 1.000000? Last edited by RenFromPenn; 12-12-2008 at 12:33 PM. |
| RenFromPenn is offline | |
| | #9 |
| Banned Join Date: Dec 2008 Location: Maputo, Mozambique
Posts: 82
| Code: #include <stdio.h>
void liquid(float, float, float, float);
int main(void)
{
int remainder, input;
float input, gallons, quarts, pints, cups;
printf("How many cups do you have?: \n");
scanf("%d",&input);
gallons = (float)input/16.0f;
remainder = input % 16;
quarts = (float)remainder/4.0f;
remainder = remainder % 4;
pints = (float)remainder/2.0f;
remainder = remainder % 2;
cups = remainder;
liquid(&gallons, &quarts, &pints, &cups);
return 0;
}
void liquid(float gallons, float quarts, float pints, float cups)
{
printf("You have %.0f gallon(s).\n",gallons);
printf("You have %.0f quart(s).\n",quarts);
printf("You have %.0f pint(s).\n",pints);
printf("You have %.0f cup(s).\n",cups);
return;
}
Last edited by c++0x; 12-12-2008 at 12:41 PM. |
| c++0x is offline | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Set your precision to 0, a la %.0f. Or you could use the proper data type for the job, and change everything else to ints too. |
| tabstop is offline | |
| | #11 |
| Registered User Join Date: Nov 2008
Posts: 16
| I don't understand what you mean by "Set your precision to 0, a la %.0f." Could you post an example using my code so that I can see what you mean? Also, I tried changing everything to int and the program wouldn't work at all. I just ended up with error messages. Are you saying that it should work if every float is changed to an int? |
| RenFromPenn is offline | |
| | #13 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If every float is changed to an int (and of course you use %d everywhere) then it will work much better yes. And I don't know what you don't understand about "change %f to %.0f". |
| tabstop is offline | |
![]() |
| Tags |
| function, modulus, remainder |
| Thread Tools | |
| Display Modes | |
|