Need some help with some code

This is a discussion on Need some help with some code within the C Programming forums, part of the General Programming Boards category; Here is my program... cant get the square meters and square feet to calculate. What am i doing wrong. I ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    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. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    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. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    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. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    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. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote 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
    Last edited by Darryl; 04-13-2006 at 08:30 PM.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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