Thread: Structure Programming ---- Please Help

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Structure Programming ---- Please Help

    Define the structure yardsfeetinches with three simple variables int yards, int feet and float inches to declare distances in yards, feet and inches.

    Write a program that adds two yardsfeetinches distances and store the result in third yardsfeetinches distance. Remember to make necessary inch to feet and feet to yard conversions. Note: 12 inches = 1 foot and 3 feet = 1 yard.

    a.out
    This program continues the introduction to structures.

    Please enter yard, feet and inches: 1 2 3
    1 yd 2 ft 3.00 + 2 yd 2 ft 9.75=4yd 2ft 0.75

    Declare a second structure area which has two structure variables yardsfeetinches length and yardsfeetinches width. Write a program to calculate the area of a rectangle using the type area.

    This is where I am at:

    Code:
    #include <stdio.h>
    
    typedef struct{
    int yards;
    int feet;
    float inches;
    }yardsfeetinches;
    
    
    int main(void){
    yardsfeetinches y;
    yardsfeetinches y1;
    yardsfeetinches y2;
    float total;
    
    y1.yards = 2;
    y1.feet = 2;
    y1.inches = 9.75;
    
    printf("\nThis program continues the introduction to structures.");
    
    printf("\n\nPlease enter yards, feet, and inches: ");
    scanf("&#37;d%d%f", &y2.yards, &y2.feet, &y2.inches);
    
    y.yards = y1.yards + y2.yards;
    y.feet = y1.feet + y2.feet;
    y.inches = y1.inches + y2.inches;
    
    total = y.yards + y.feet * 0.33 + y.inches * 0.03;
    
    y.yards = total/1.17;
    y.feet = total/2.35;
    y.inches = total/6.27;
    
    printf("\n%d yd %d ft %f + 2 yd 2 ft 9.75 = ", y2.yards, y2.feet, y2.inches);
    printf("%d yd %d ft %f\n", y.yards, y.feet, y.inches);
    
    }

    Really could use some help on this

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    total probably should contain total number of yards, so why do you need the /1.17 part?

    After you have stored some yards to y.yards - you probably want to decrease the total before calculating feet

    As a side note - you should indent your code properly, and check the return value of scanf - you cannot be sure that the user enters values correcly, so without checking - you can start work on garbage
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    It would be great if you could state what is wrong rather than a generic "here is my code, help".

    What is the program output? Does it compile? Do you get any errors? How do you know it is not working?

  4. #4
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I would calculate your total in total inches and as vart said you need to modify total after you calculate the number of yards. I'm not sure what your weird divisors are for (1.17, 2.35, 6.27).

    Code:
    total = y.yards * 36 + y.feet * 12 + y.inches;
    
    y.yards = total / 36;
    total -= y.yards * 36;
    
    y.feet = total / 12;
    total -= y.feet * 12;
    
    y.inches = total;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM