# Thread: Need some help with some code

1. ## Need some help with some code

Here is my program... cant get the square meters and square feet to calculate. What am i doing wrong. I appreciate any help you can give....

not homework just an excercise.

#include <stdio.h>

Code:
```int main()
{

/* Vaible declarations */
int cv_factor=36*36;	/* Conversion factor to convert  */
int length_yds;				/* Length in yards; input by user  */
int width_yds;				/* Width in yards; input by user  */
int area_in_yards;
int area_in_feet;
int square_yards;
int square_feet;
int square_inches;
int square_meters;
int area_in_inches;
int feet_cv_factor=3*3;		/* Conversion factor to convert  */
/* Accept user input for length and width of lawn in yards  */
printf("input length and width of lawn in yards  ");
scanf("%d%d", &length_yds, &width_yds);
/* Compute area of lawn in square yards, square feet, square inches and square meters */
area_in_yards= (length_yds * width_yds);
area_in_feet= (area_in_yards * feet_cv_factor);
square_inches= (area_in_yards * cv_factor);
square_feet= (area_in_yards * 9);
square_meters= (area_in_yards * 0.83612736);
printf("\Lawn in square yards = %d\n", area_in_yards);
printf("Lawn in square feet = %d\n", square_feet);
printf("\Lawn in square inches = %d\n", square_inches);
printf("\Lawn in square meters = %d\n", square_meters);
return 0;
}```

2. Code:
`scanf("%d%d", &length_yds, &width_yds);`
Make that two seperate scanfs.
Code:
```scanf("%d", &length_yds);
scanf("%d", &width_yds);```
Also you might wanna make some of those into floating point variables.

3. Works for me?

Output:
Code:
```input length and width of lawn in yards  10 10
Lawn in square yards = 100
Lawn in square feet = 900
Lawn in square inches = 129600
Lawn in square meters = 83
Press any key to continue . . .```

4. Are you saying that when you run my code and enter 10 10 it works for you. On my computer using visual studio 2003 , lawn in square feet shows 0 and lawn in square meters shows 0. Did you do any adjustments

5. Thanks guys... it was driving me crazy. i have a bunch of excercises to do and so little time to do them.. at my slow rate of gettting the program to the point above i should be done in a year.... thanks again for the help...

6. Originally Posted by Mshock
Are you saying that when you run my code and enter 10 10 it works for you. On my computer using visual studio 2003 , lawn in square feet shows 0 and lawn in square meters shows 0. Did you do any adjustments
That's what I am saying, no adjustments, I'm using vs2005

Maybe there's a difference if you compile it as c++ or c. I used c++ but since you posted in the c forum I am guessing you compiled as c

7. In general the code should work fine ( I have no Idea if the conversion-factors are right but I assume they are ).
There is a problem with the conversion to square meters. You are using ints to hold the results. so
Code:
`square_meters= (area_in_yards * 0.83612736);`
will not calculate an accurate value.
The expression (area_in_yards * 0.83612736) will produce a double and this will be converted to an int by just discarding ( not rounding ) the digits after the decimal point. That's why for an input of 1x1 yards you will get a result of 0 square-meters.
Kurt