C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-12-2008, 12:00 PM   #1
Registered User
 
Join Date: Nov 2008
Posts: 16
Need help with liquid measurements using functions and remainders.

Can someone please tell me what is wrong with this program? it is supposed to output 1 gallon, 2 quarts, 1 pint, and 1 cup. The math isn't right on the gallons, It is displaying some strange decimal that seems to go on forever. It also isn't printing anything for quarts, pints, and cups. Help!

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   Reply With Quote
Old 12-12-2008, 12:15 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
You can't use %f to read into an integer variable.
tabstop is offline   Reply With Quote
Old 12-12-2008, 12:17 PM   #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   Reply With Quote
Old 12-12-2008, 12:21 PM   #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   Reply With Quote
Old 12-12-2008, 12:22 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by RenFromPenn View Post
I tried using %d and it displayed nothing but zeros. Please help me fix this.
You have an interesting idea of what "all zeros" mean.
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).
(After changing to %d.)
tabstop is offline   Reply With Quote
Old 12-12-2008, 12:24 PM   #6
Registered User
 
Join Date: Nov 2008
Posts: 16
Quote:
Originally Posted by tabstop View Post
You have an interesting idea of what "all zeros" mean.
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).
(After changing to %d.)
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   Reply With Quote
Old 12-12-2008, 12:27 PM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by RenFromPenn View Post
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?
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   Reply With Quote
Old 12-12-2008, 12:29 PM   #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   Reply With Quote
Old 12-12-2008, 12:33 PM   #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;
}
Wurks 2.

Last edited by c++0x; 12-12-2008 at 12:41 PM.
c++0x is offline   Reply With Quote
Old 12-12-2008, 12:38 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by RenFromPenn View Post
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?
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   Reply With Quote
Old 12-12-2008, 12:43 PM   #11
Registered User
 
Join Date: Nov 2008
Posts: 16
Quote:
Originally Posted by tabstop View Post
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.
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   Reply With Quote
Old 12-12-2008, 12:46 PM   #12
Banned
 
Join Date: Dec 2008
Location: Maputo, Mozambique
Posts: 82
I found a great example of this here. If you are interested.
c++0x is offline   Reply With Quote
Old 12-12-2008, 12:53 PM   #13
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Reply

Tags
function, modulus, remainder

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:30 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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