Thread: Simple C woes

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    12

    Simple C woes


    i'm having trouble with this C code. I need a .XXX for secs ...but since i use the % (modulo) it throws it away. How can i get this . xxx. here is my code:

    #include<stdio.h>
    int main(void) {
    int area_house, area_yard, length_yard, width_yard;
    int length_house, width_house,sec;
    double secs, yard_size,hours, mins, time;
    /*Area of yard*/
    printf("\nWhat is the length and width of the yard:");
    scanf("%d%d",&length_yard, &width_yard);

    area_yard = length_yard*width_yard;

    /*Area of house*/
    printf("\nWhat is the length and width of the house:");
    scanf("%d%d", &length_house, &width_house);

    area_house = length_house*width_house;

    yard_size = area_yard-area_house;

    time = yard_size/2.0;
    /* convert the seconds to hours, minutes, seconds. */
    sec = time ;
    secs = sec % 60;
    mins = (time / 60.0);
    hours = (mins/ 60.0);


    printf("\nIt will take %.0lf hours, %.0lf minutes, %.3lf\nto mow a lawn of %.0lf square feet.\n", hours, mins, secs,yard_size);

    return (0);
    }

    any help is welcomed.
    -gman932
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,656
    Rep Power
    4248
    Try this:
    secs = (sec % 60) + (time - sec);
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    12

    worked but....


    thx for your help Scorpions4ever, however my code is seriously flawed......I need to get minutes under 60 too. 2hours XX minutes XX.XXX sec ....don't like having over 60 minutes :/.

    can anyone please debug it. I have spent way to much time to get crappy solution. An explanation would help me too :) I need to learn too :).

    Thank you
    gman
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,656
    Rep Power
    4248
    Try using the gmtime() function. This converts seconds to years, months, days, hours, min, secs etc.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
      int area_house, area_yard, length_yard, width_yard;
      int length_house, width_house,sec;
      double secs, yard_size, time;
      struct tm *foo;
    
      /*Area of yard*/
      printf("\nWhat is the length and width of the yard:");
      scanf("%d%d",&length_yard, &width_yard);
    
      area_yard = length_yard*width_yard;
    
      /*Area of house*/
      printf("\nWhat is the length and width of the house:");
      scanf("%d%d", &length_house, &width_house);
    
      area_house = length_house*width_house;
    
      yard_size = area_yard-area_house;
    
      time = yard_size/2.0;
      /* convert the seconds to hours, minutes, seconds. */
      sec = time;
      secs = (sec % 60) + (time - sec);
      foo = gmtime((time_t *)&sec);
    
      printf("\nIt will take %.0lf hours, %.0lf minutes, %.3lf\nto mow a lawn of %.0lf square feet.\n",
             (float)foo->tm_hour, (float)foo->tm_min,
             secs,yard_size);
    
      return (0);
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    12
    I could not get that code to compile using gcc or miracle C. I'm sure there is an easier way of doing it though. Thank you for your help, but the problem is still unsolved.
    -gman932
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    12
    scorpions4ever, thx for your input.
    Seeing you are the only one to reply to my post. I have resolved the problem by using the following code:

    #include<stdio.h>


    int main(void) {
    int area_house, area_yard, length_yard, width_yard;
    int length_house, min, width_house,sec, mins, hours;
    double secs, yard_size, time;

    /*Area of yard*/
    printf("\nEnter the length and width of the yard:");
    scanf("%d%d",&length_yard, &width_yard);

    area_yard = length_yard*width_yard;

    /*Area of house*/
    printf("\nEnter the length and width of the house:");
    scanf("%d%d", &length_house, &width_house);

    area_house = length_house*width_house;

    yard_size = area_yard-area_house;

    time = yard_size/2.0;
    /* convert the seconds to hours, minutes, seconds. */
    sec = time ;
    secs = (sec % 60) + (time - sec);
    mins = (sec / 60);
    hours = ( mins / 60);
    mins = (mins % 60);



    printf("\nIt will take %d hours, %d minutes, %.3lf\nto mow a lawn of %.0lf square feet.\n", hours, mins, secs, yard_size);

IMN logo majestic logo threadwatch logo seochat tools logo