# 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. ## 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. > light_f = (float) yards * (light / 100);
yards is already a float, the cast is useless there.
Try casting the light.

3. 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);```

4. Use narf's post - much better

5. 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;
}```