Calculations not working....

This is a discussion on Calculations not working.... within the C Programming forums, part of the General Programming Boards category; My result is listed as: "You will need 0.00 yards of light, 0.00 yards of medium, and 0.00 yards of ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Calculations not working....

    My result is listed as:

    "You will need 0.00 yards of light, 0.00 yards of medium, and 0.00 yards of dark fabric for your quilt."

    No matter the values entered, answers are always 0.00. What am I doing wrong?

    Code:
    /*  Quilting Fabric Calculator  */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {	
    	int light, medium, dark;
    	float yards, light_f, medium_f, dark_f;
    
    	printf("Percent of quilt that will consist of light fabrics: ");
    	scanf("%d", &light);
    	
    	printf("\nPercent of quilt that will consist of medium fabrics: ");
    	scanf("%d", &medium);
    	
    	printf("\nPercent of quilt that will consist of dark fabrics (all should total 100): ");
    	scanf("%d", &dark);
    	
    	printf("\nEnter yards of fabric: ");
    	scanf("%f", &yards);	
    	
    	light_f = (float) yards * (light / 100);
    
    	medium_f = (float) yards * (medium / 100);
    	
    	dark_f = (float) yards * (dark / 100);
    	
    	printf("\n\nYou will need %.2f yards of light, %.2f yards of medium, and %.2f yards of dark fabric for your quilt.\n\n",
     	light_f, medium_f, dark_f);
     			
     	printf("\nPress Enter to continue...\n");
    	getchar();
    	
    	return 0;
    }
    Hope this is straight-forward.

    Thanks!
    Chris

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > light_f = (float) yards * (light / 100);
    yards is already a float, the cast is useless there.
    Try casting the light.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    light, medium, and dark are integers. So is 100, so using integral division, any precision is chopped off, giving you 0. You can fix it like this:
    Code:
    light_f = yards * (light / 100.0f);
    medium_f = yards * (medium / 100.0f);
    dark_f = yards * (dark / 100.0f);
    Just because I don't care doesn't mean I don't understand.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Use narf's post - much better
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Thank you for your responses! I used Narf's and it works fine now. I am just learning C, still getting used to using "(float)" properly and such.

    Thought I would challenge myself to come up with a C program that accomplishes the same thing as: http://search.quiltshops.com/calculator/calculator.htm
    (my wife suggested it when I asked for an idea, she uses the site)

    Here is the entire working program if anyone is interested (I omitted the parts above that were uneeded to pose my question):

    Code:
    /*  Quilting Fabric Calculator  */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int block_w, block_h, block_a, block_d, fabric, area, fab_a, light, medium, dark, run;
    	float yards, fab_d, light_f, medium_f, dark_f;
    	char choice;
    
    	do{
    		clrscr();
    		printf("Welcome to the Quilting Fabric Calculator\n");
    		printf("This program will tell you how much fabric\n");
    		printf("you will need to construct your quilt.\n");
    		printf("(press 1 to continue, 0 to exit): ");
    		scanf("%d", &run);
    
    		if(run == 1){
    			printf("\nEnter the width of each block (including seam allowance, in inches): ");
    			scanf("%d", &block_w);
    	
    			printf("\nEnter the height of each block (including seam allowance, in inches): ");
    			scanf("%d", &block_h);
    	
    			printf("\nEnter the number of blocks across quilt: ");
    			scanf("%d", &block_a);
    	
    			printf("\nEnter the number of blocks down quilt: ");
    			scanf("%d", &block_d);
    	
    			printf("\nEnter the fabric width (in inches): ");
    			scanf("%d", &fabric);
    	
    			area = block_a * block_d;
    			fabric /= block_w;
    			fab_a = fabric;
    			fab_d = (float) area / fab_a;
    			yards = (float) (fab_d * block_h) / 36;
    	
    			printf("\n\nYou will need %.2f yards of fabric for your quilt!", yards);
    	
    			printf("\n\nWould you like me to calculate how much light/medium/dark fabric you will need (y/n)?");
    			scanf("%c", &choice);
    
    			if(choice == 'y'){
    				printf("\n\nPercent of quilt that will consist of light fabrics: ");
    				scanf("%d", &light);
    	
    				printf("\nPercent of quilt that will consist of medium fabrics: ");
    				scanf("%d", &medium);
    	
    				printf("\nPercent of quilt that will consist of dark fabrics (all should total 100): ");
    				scanf("%d", &dark);
    	
    				light_f = yards * (light / 100.0f);
    
    				medium_f = yards * (medium / 100.0f);
    	
    				dark_f = yards * (dark / 100.0f);
    	
    				printf("\nYou will need %.2f yards of light, %.2f yards of medium, and %.2f yards of dark fabric for your quilt.\n\n",
     				light_f, medium_f, dark_f);
     				
     				printf("\nPress Enter to continue...\n");
    				getchar();
    			}
    		}
    	}while(run == 1);
    
    	return 0;
    }
    Thanks again for your help!
    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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