Thread: forward declaration error

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    forward declaration error

    Code:
    // code to solve elapsed time between two entered times
    
    #include <stdio.h>
    
    
        struct time
        {
             int hr;
             int min;
             int sec;
        };
    
    
    int elapsed_time (struct time1, struct time2)
    {
        struct time time1, time2, diff;
        
        diff.hr = time2.hr - time1.hr;
        if (time2.hr < time1.hr)
           diff.hr = diff.hr + 12;
     
        diff.min = time2.min - time1.min;
        if (time2.min < time1.min)
           diff.min = diff.min + 60;
          
        diff.sec = time2.sec - time1.sec; 
        if (time2.sec < time1.sec)
           diff.sec = diff.sec + 60;
          
        return diff;
    }
    int main (void)
    {
        int stopit;
         
        struct time diff, time1, time2;
        int elapsed_time (struct time1, struct time2);
        
        printf ("Enter a military time (i.e. 24 hour basis) in the format hh:mm:ss\n");
        scanf ("%i:%i:%i", &time1.hr, &time1.min, &time1.sec);
        
        printf ("Enter a second military time using the same format.\n");    
        scanf ("%i:%i:%i", &time2.hr, &time2.min, &time2.sec);
        
        diff = elapsed_time (time1, time2);
        
        printf ("The elapsed time between the two times entered is %.2i:%.2i:%.2i\n", diff.hr, diff.min, diff.sec);
        scanf ("%d", &stopit);
    return 0;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    int elapsed_time (struct time1, struct time2)
    You are not giving the full data type... It should be
    Code:
    int elapsed_time (struct time time1, struct time time2)
    But even if you did that, your function will have the same variable names as your parameters. ex) line 16.
    Either you want to change those names, or just remove them all together now since you are passing in those structures.

    Line 37 does not need to be there since you have declared your function definitions above main.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have you even bothered to read the error messages emitted by your compiler???? Or are you (as I suspect) just hacking code without much thought, and expecting the compiler to cope anyway?

    You probably could have solved the problem yourself by more carefully reading your compiler's error messages. It would have also been courteous to supply information in your post about where the errors were reported.

    In any event, this line
    Code:
    int elapsed_time (struct time1, struct time2)
    tells the compiler that elapsed_time() accepts an argument of type "struct time1" and an argument of type "struct time2" (the time1 and time2 are part of the type NOT the argument name). Nowhere in your code is a "struct time1" or a "struct time2" defined. You have repeated this error inside main().

    Although the compiler won't complain, the first two lines following
    Code:
    {
        struct time time1, time2, diff;
    declare three variables (all of type "struct time") named time1, time2, and diff. time1 and time2 have no relationship whatsoever to the time1 and time2 specified in the function argument list.

    Then at the end of elapsed_time() your code does
    Code:
         return diff;
    which won't work, since (1) elapsed_time has been specified as returning int (2) diff is of type "struct time" and (3) there is no way the compiler has to convert a "struct time" into an int.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by beebee View Post
    Code:
    // code to solve elapsed time between two entered times
    Forum read error: 28: reached end of post without encountering any question or articulated problem

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Sorry Grumpy. I am a real beginner...taking my first course in C. I modified the program (see below) and now have a compiler error "declaration of 'time x' shadows a parameter. This has me stumped as well. Any help would be appreciated.
    Code:
    #include <stdio.h>
    
    
        struct time
        {
             int hr;
             int min;
             int sec;
        };
    
    
    int elapsed_time (struct time x, struct time y)
    {
        struct time x, y, diff;
        
        diff.hr = y.hr - x.hr;
        if (y.hr < x.hr)
           diff.hr = diff.hr + 12;
     
        diff.min = y.min - x.min;
        if (y.min < x.min)
           diff.min = diff.min + 60;
          
        diff.sec = y.sec - x.sec; 
        if (y.sec < x.sec)
           diff.sec = diff.sec + 60;
           
            printf ("The elapsed time between the two times entered is %.2i:%.2i:%.2i\n", diff.hr, diff.min, diff.sec);
    }
    int main (void)
    {
        int stopit;
        
        
        struct time diff, time1, time2;
        int elapsed_time (struct time time1, struct time time2);
        
        printf ("Enter a military time (i.e. 24 hour basis) in the format hh:mm:ss\n");
        scanf ("%i:%i:%i", &time1.hr, &time1.min, &time1.sec);
        
        printf ("Enter a second military time using the same format.\n");    
        scanf ("%i:%i:%i", &time2.hr, &time2.min, &time2.sec);
        
        diff = elapsed_time (time1, time2);
        scanf ("%d", &stopit);
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look at the first three lines of the elapsed_time() function, You will see two distinct things named x, and two distinct things named y. That is shadowing.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Thank you for your help. The program compiles! It doesn't run, but it compiles. 1/2 way there.
    beebee

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by beebee View Post
    Thank you for your help. The program compiles! It doesn't run, but it compiles. 1/2 way there.
    beebee
    I vote for 1/3 of the way there.
    1/3 Compiles
    2/3 Links and Runs
    3/3 Runs and does what you want it to do.

    I just wish the last third did not take more than a third of the work, sometimes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to use forward declaration
    By afflictedd2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2011, 12:15 PM
  2. forward declaration error?
    By sugarfree in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 12:11 AM
  3. Forward declaration with g++
    By blakaxe in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2009, 11:44 AM
  4. Forward declaration with g++
    By blakaxe in forum Linux Programming
    Replies: 0
    Last Post: 06-15-2009, 09:22 AM
  5. forward declaration
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 01-29-2009, 07:13 PM