Thread: Need help with liquid measurements using functions and remainders.

  1. #1
    Deleted Account
    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;
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't use %f to read into an integer variable.

  3. #3
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I tried using &#37;d and it displayed nothing but zeros. Please help me fix this.

  4. #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("&#37;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;
        }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  6. #6
    Deleted Account
    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?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  8. #8
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    So, I just change it for scanf("&#37;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 deleted_user; 12-12-2008 at 12:33 PM.

  9. #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("&#37;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.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  11. #11
    Deleted Account
    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?

  12. #12
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    I found a great example of this here. If you are interested.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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".

Popular pages Recent additions subscribe to a feed

Tags for this Thread