help with output

This is a discussion on help with output within the C Programming forums, part of the General Programming Boards category; I'm writing a program that takes the length and width of a rectangular yard and house then computes the time ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    help with output

    I'm writing a program that takes the length and width of a rectangular yard and house then computes the time required to cut the grass at two square feet a second. The final output my program gives is "NaN". Does anyone have any advice how I could write this better and produce better results? Thanks in advance. Here is what I have so far:

    Code:
        /*
             Filename: mow.c
             Description: Compute the required time to cut grass at two square 
                          feet a second.
         */
    
    
    #include <stdio.h>
    #include <math.h>
    
     int main()
    
      {
    
          /* memory allocation for data */
    
         double ylength,ywidth,hlength,hwidth,yarea,harea,grasqyd,grasqft,time;
    
    
          /* prompt user for data */
    
      printf ("Enter the length of the yard in feet (eg.20):";
      scanf  ("%lf",& ylength);
    
      printf ("Enter the width of the yard in feet (eg.20):");
      scanf  ("%lf",& ywidth);
    
      printf ("Enter the length of the house in feet (eg.20):");
      scanf  ("%d",& hlength);
    
      printf ("Enter the width of the house in feet (eg.20):");
      scanf  ("%lf",& hwidth);
    
    
           /* compute the total area of grass */
    
             yarea = ylength * ywidth;
    
             harea = hlength * hwidth;
    
             grasqyd = yarea - harea;
    
             grasqft = grasqyd * 9;
    
    
           /* compute the time required */
    
             time = grasqft/2;
    
                   
           /* display result */
    
      printf
    
     ("The time required to cut the grass at a rate of two square feet a\n" 
      "second is %f \n",time);
    
           /* End */
    
     return 0;
    
      }

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Try calling getchar() after each call to scanf().
    Code:
    scanf  ("%lf",& ylength);
    getchar();
    And so on...

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    No idea why csisz3r thinks you should add a getchar() after each call.

    The third scanf (hlength) uses %d, but hlength is a double, so use %lf like the others.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    time is the name of a function in <time.h>. Call your variable something else.

    NaN means Not A Number. Usually it means an incredibly large or small number, or an error of some sort.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thanks for both your responses. Now when I run the program I get "-2655.000000" seconds with the numbers I enter. What am I missing? How can I show better more realistic times?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You changed the %d to %lf?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    "second is %f \n",time);
    And make that %lf.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    yes...I change these to %lf

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    is there a way to display this without using time.h?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    What? What I meant was that if you include time.h, you wouldn't be able to access time() from inside main().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Let's see your newest source.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    He's just pointing out that it's bad style to name a variable after something that already exists in the standard library - if you correct those formatting characters in your scanf and printf calls, your program should work.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    OK...here is my new source code:

    Code:
         /*
             Filename: mow.c
             Description: Compute the required time to cut grass at two square 
                          feet a second.
         */
    
    
    #include <stdio.h>
    #include <math.h>
    
     int main()
    
      {
    
          /* memory allocation for data */
    
         double ylength,ywidth,hlength,hwidth,yarea,harea,grasqyd,grasqft,time;
    
    
          /* prompt user for data */
    
      printf ("Enter the length of the yard in feet (eg.20):");
      scanf  ("%lf",& ylength);
    
      printf ("Enter the width of the yard in feet (eg.20):");
      scanf  ("%lf",& ywidth);
    
      printf ("Enter the length of the house in feet (eg.20):");
      scanf  ("%lf",& hlength);
    
      printf ("Enter the width of the house in feet (eg.20):");
      scanf  ("%lf",& hwidth);
    
    
           /* compute the total area of grass */
    
             yarea = ylength * ywidth;
    
             harea = hlength * hwidth;
    
             grasqyd = yarea - harea;
    
             grasqft = grasqyd * 9;
    
    
           /* compute the time required */
    
             time = grasqft/2;
    
                   
           /* display result */
    
      printf
    
     ("The time required to cut the grass at a rate of two square feet a\n" 
      "second is %f seconds.\n",time);
    
           /* End */
    
     return 0;
    
      }

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    "second is %f seconds.\n",time);
    I already told you to change that to %lf.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Sorry about that. I made the change but I'm still getting -2682.00000 seconds for an answer.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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