Thread: Unused variables? (short code)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Unused variables? (short code)

    Code:
     #include <stdio.h>
      4 #define mile 5280
      5 #define kilometer 3282
      6 void instructions (void);
      7 int main()
      8 {
      9         int minutes;
     10         scanf("%d", &minutes);
     11         double seconds;
     12         scanf("%lf", &seconds);
     13         double totsec=(60*minutes)+(seconds);
     14         double fps =(mile)/(totsec);
     15         double mps=(1000.0/kilometer)*(5280.0/totsec);
     16         printf(" That is %.1lf feet per second, and %.2f meters per second.\    n",fps,mps);
     17
     18         return 0;
     19 }
     20
     21         void instructions()
     22         {
     23                 printf("This program will ask for the minutes and seconds fo    r the time it\ntook for a runner to run a mile.  The program will then calcu    late\nthe feet per second and meters per second for that runner.\n");
     24
     25
     26
     27         printf("\nMinutes for the runner: ");
     28         printf("Seconds for the runner: ");
     29
     30 }

    It compiled fine. But when i run a.out, the program doesn't display any text nor prompt the user for input.
    But as grumpy correctly pointed out. I cannot nest a function within main.

    My goal is to ("Write and call a function that displays instructions to the program user".)
    Last edited by tmac619619; 10-13-2012 at 06:11 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    C does not support nesting functions inside functions.

    There are probably error messages you haven't seen fit to mention, but the compiler will be emitting them because you have nested instructions() inside the body of main().

    The compiler will probably be horribly confused after that, when compiling subsequent code. The errors and warnings given may or may not be valid.

    Depending on age of your C compiler, variables must be defined at the top of blocks (before any executable statements).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    So what you're is i should put my calcuations, declared variables under main.

    And then put the void instructions function outside of main?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tmac619619 View Post
    So what you're is i should put my calcuations, declared variables under main.
    What you've said here is as clear as mud.


    Quote Originally Posted by tmac619619 View Post
    And then put the void instructions function outside of main?
    That is a corollary of the fact that function cannot be implemented inside functions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    I see your point grumpy.

    basically main cannot support nested functions.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Good news. Program compiled fine, but it doesn't prompt the user for input.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well where in your main do you call instructions?

    As in
    Code:
    int main ( ) {
        instructions();
        // rest of code
    }
    Also, to prevent long lines, do this
    Code:
    printf("This program will ask for the minutes and seconds for the time it\n"
           "took for a runner to run a mile.  The program will then calculate\n"
           "the feet per second and meters per second for that runner.\n");
    The compiler will automatically join the strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just another hint to the OP - if you change your code, it is better to create a second post with the new code, and not edit your original code.


    The original code you posted had the definition (implementation) of instructions() within the body of main(). However, you have since edited the first post and corrected that, so my subsequent replies appear without the context in which I wrote them.


    Even though the definitions of functions cannot be nested, it is still necessary to write code to call your functions, as in Salem's post.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    OOOOOps, I finally find the reason why I cannot understand this whole post....

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    There's no need for the second void instruction() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove unused code - is there any tools?
    By Hannibal2010 in forum C Programming
    Replies: 5
    Last Post: 06-27-2011, 01:36 AM
  2. Passing unused variables to a function
    By TuxRules in forum C Programming
    Replies: 2
    Last Post: 03-07-2010, 02:09 PM
  3. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  4. Do compilers remove unused code?
    By tretton in forum C Programming
    Replies: 6
    Last Post: 12-27-2005, 12:53 AM
  5. how will this short c++ code look in pure c?
    By dune911 in forum C Programming
    Replies: 8
    Last Post: 10-19-2001, 11:21 AM