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

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Exclamation Struct Problem Please help


    if ( dt.stime.hour == 0 && dt.stime.minutes == 0 && dt.stime.seconds == 0 )
    dt.sdate = dateUpdate (dt.sdate);

    return dt;
    }

    int main (void)
    {
    struct dateAndTime dt1 = { { 12, 31, 2004 }, { 23, 59, 59 } };
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by cougarsmustangs
    But, when I compile its showing bunch of errors that I couldn't seem to fix. Any help would be greatly appreciated.
    The code is extremely messy. You may not get the help you expect with this kind of code. If you can just re-write the whole things (making sure you are declaring the structures and data types correctly) then you'l get a lot of help.... sorry!!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by eVil poIntErr..
    The code is extremely messy. You may not get the help you expect with this kind of code. If you can just re-write the whole things (making sure you are declaring the structures and data types correctly) then you'l get a lot of help.... sorry!!!
    what about now? Any help is appreciated

    Code:
    #include <stdio.h>
    
    
    struct dateAndTime  clockKeeper (struct dateAndTime  dt)
    {
         struct time  timeUpdate (struct time  now);
         struct date  dateUpdate (struct date  today);
    
         dt.stime = timeUpdate (dt.stime);
    
    	if ( dt.stime.hour == 0  &&  dt.stime.minutes == 0  &&
                 dt.stime.seconds == 0 )
    	     dt.sdate = dateUpdate (dt.sdate);
    
       	return  dt;
    }
    
    
    int main (void)
    {
     struct dateAndTime  dt1 = { { 12, 31, 2004 }, { 23, 59, 59 } };
     struct dateAndTime  dt2 = { { 2,  28, 2008 }, { 23, 59, 58 } };
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n",
    	    dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
    	    dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);
    
        dt1 = clockKeeper (dt1);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n",
    	    dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
    	    dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n"
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        dt2 = clockKeeper (dt2);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        dt2 = clockKeeper (dt2);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n"
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        return 0;
    
    }
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > dt.stime = timeUpdate (dt.stime);
    > dt.sdate = dateUpdate (dt.sdate);
    So who's job is it to write timeUpdate() and dateUpdate() ?

    Next time, post your actual error messages and not "there are some errors".
    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. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    > dt.stime = timeUpdate (dt.stime);
    > dt.sdate = dateUpdate (dt.sdate);
    So who's job is it to write timeUpdate() and dateUpdate() ?

    Next time, post your actual error messages and not "there are some errors".
    this is what i got as errors.
    How do you write timeUpdate() and dateUpdate() ? I am trying to teach myself how to program. This is one of the exercise problems in the book that I use.

    Code:
    $ vim ex9.5.c
      
    $ gcc ex9.5.c
    ex9.5.c:17:53: error: parameter 1 ('dt') has incomplete type
    ex9.5.c:17:21: error: return type is an incomplete type
    ex9.5.c: In function 'clockKeeper':
    ex9.5.c:28:5: warning: 'return' with a value, in function returning void [enable
    d by default]
    ex9.5.c: In function 'main':
    ex9.5.c:33:12: error: variable 'dt1' has initializer but incomplete type
    ex9.5.c:33:12: error: extra brace group at end of initializer
    ex9.5.c:33:12: error: (near initialization for 'dt1')
    ex9.5.c:33:12: warning: excess elements in struct initializer [enabled by defaul
    t]
    ex9.5.c:33:12: warning: (near initialization for 'dt1') [enabled by default]
    ex9.5.c:33:12: error: extra brace group at end of initializer
    ex9.5.c:33:12: error: (near initialization for 'dt1')
    ex9.5.c:33:12: warning: excess elements in struct initializer [enabled by defaul
    t]
    ex9.5.c:33:12: warning: (near initialization for 'dt1') [enabled by default]
    ex9.5.c:33:25: error: storage size of 'dt1' isn't known
    ex9.5.c:34:12: error: variable 'dt2' has initializer but incomplete type
    ex9.5.c:34:12: error: extra brace group at end of initializer
    ex9.5.c:34:12: error: (near initialization for 'dt2')
    ex9.5.c:34:12: warning: excess elements in struct initializer [enabled by defaul
    t]
    ex9.5.c:34:12: warning: (near initialization for 'dt2') [enabled by default]
    ex9.5.c:34:12: error: extra brace group at end of initializer
    ex9.5.c:34:12: error: (near initialization for 'dt2')
    ex9.5.c:34:12: warning: excess elements in struct initializer [enabled by defaul
    t]
    ex9.5.c:34:12: warning: (near initialization for 'dt2') [enabled by default]
    ex9.5.c:34:25: error: storage size of 'dt2' isn't known
    ex9.5.c:37:6: error: invalid operands to binary % (have 'char *' and 'complex do
    uble')
    ex9.5.c:37:10: error: expected ')' before ':' token
    ex9.5.c:37:10: error: stray '\' in program
    ex9.5.c:37:22: warning: missing terminating " character [enabled by default]
    ex9.5.c:37:10: error: missing terminating " character
    ex9.5.c:51:6: error: expected ')' before 'dt2'
    ex9.5.c:70:6: error: expected ')' before 'dt2'
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > How do you write timeUpdate() and dateUpdate() ?
    The same way you wrote clockKeeper()
    - you identified an aspect of functionality
    - you created an interface (parameters and return result)
    - you implemented some functionality within the function.

    > dt.stime = timeUpdate (dt.stime);
    I mean, when you wrote this line, what were you thinking?
    - timeUpdate() magically pops into existence and my work is done.

    How about adding stub functions for all the things you know you need to write at some point.
    Code:
    struct time  timeUpdate (struct time  now) {
        printf("Dummy timeUpdate\n");
        return now;
    }
    You also seem to have forgotten to add one of these as well.
    Code:
    struct dateAndTime {
      struct date sdate;
      struct time stime;
    };

    Comments on this post

    • mythos_ agrees
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    25
    Rep Power
    0
    First:

    structs are heaps easier to read in code if you use typdef:
    Code:
    typedef struct dateTime {
    //def of the struct
    } dateTime;
    
    //from now on use "dateTime" instead of "struct dateTime"
    second:

    i think you declare the functions timeUpdate and dateUpdate within a function. Why is that?
    Better define it above, directly after definition of struct dateAndTime (which is missing too...).
    Declaring but never defining makes the linker angry :flame:
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    > How do you write timeUpdate() and dateUpdate() ?
    The same way you wrote clockKeeper()
    - you identified an aspect of functionality
    - you created an interface (parameters and return result)
    - you implemented some functionality within the function.

    > dt.stime = timeUpdate (dt.stime);
    I mean, when you wrote this line, what were you thinking?
    - timeUpdate() magically pops into existence and my work is done.

    How about adding stub functions for all the things you know you need to write at some point.
    Code:
    struct time  timeUpdate (struct time  now) {
        printf("Dummy timeUpdate\n");
        return now;
    }
    You also seem to have forgotten to add one of these as well.
    Code:
    struct dateAndTime {
      struct date sdate;
      struct time stime;
    };
    I got upto here. How do I declare now and today? And what does the last error shown in the compiler mean? And how do I fix it?

    Code:
    #include <stdio.h>
    
        int dt;
    
    struct date
    {
        int day;
        int month;
        int year;
    };
    
    struct time
    {
        int seconds;
        int minutes;
        int hour;
    };
    
    struct dateAndTime {
         struct date sdate;
         struct time stime;
         };
    
    struct dateAndTime  clockKeeper (struct dateAndTime dt)
    {
         struct time  timeUpdate (struct time  now); {
    	printf ("timeUpdate\n");
    	return now;
         }
    
         struct date  dateUpdate (struct date  today); {
    	printf ("dateUpdate\n");
    	return today;
         }    
    
         dt.stime = timeUpdate (dt.stime);
    
    	if ( dt.stime.hour == 0  &&  dt.stime.minutes == 0  &&
    	     dt.stime.seconds == 0 )
    	     dt.sdate = dateUpdate (dt.sdate);
    
       	return  dt;
    }
    
    int main (void)
    
        int dt1;
        int dt2;
    
    {
        struct dateAndTime  dt1 = { { 12, 31, 2004 }, { 23, 59, 59 } };
        struct dateAndTime  dt2 = { { 2, 28, 2008 }, { 23, 59, 58 } };
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n",
    	    dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
    	    dt1.stime.hour,
     	    dt1.stime.minutes, dt1.stime.seconds);
    
        dt1 = clockKeeper (dt1);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n",
    	    dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
    	    dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        dt2 = clockKeeper (dt2);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        printf ("Current date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        dt2 = clockKeeper (dt2);
    
        printf ("Updated date and time is %.2i/%.2i/%.2i "
    	    "%.2i:%.2i:%.2i\n\n",
    	    dt2.sdate.month, dt2.sdate.day, dt2.sdate.year,
    	    dt2.stime.hour, dt2.stime.minutes, dt2.stime.seconds);
    
        return 0;
    
    }
    Compiler errors

    Code:
    $ gcc ex9.5.c
    ex9.5.c: In function 'clockKeeper':
    ex9.5.c:28:9: error: 'now' undeclared (first use in this function)
    ex9.5.c:28:9: note: each undeclared identifier is reported only once for each fu
    nction it appears in
    ex9.5.c:33:9: error: 'today' undeclared (first use in this function)
    ex9.5.c: In function 'main':
    ex9.5.c:45:5: error: old-style parameter declarations in prototyped function def
    inition
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    25
    Rep Power
    0
    the semicolon after "struct time timeUpdate (struct time now)" finishes the statement.
    If you want to define the function, do it outside the other function and remove the semicolon.

    The way you wrote it, u declare the function, then code a block which uses the (obviously unkown) variable "now".
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by mythos_
    the semicolon after "struct time timeUpdate (struct time now)" finishes the statement.
    If you want to define the function, do it outside the other function and remove the semicolon.

    The way you wrote it, u declare the function, then code a block which uses the (obviously unkown) variable "now".
    Thank you! it worked. What's that error showing in the main function? (see the compiler error - last line)
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by cougarsmustangs
    Thank you! it worked. What's that error showing in the main function? (see the compiler error - last line)
    no need to write "void", just leave the parameters empty.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by mythos_
    no need to write "void", just leave the parameters empty.
    This is what i got when i complied. it it didn't update??
    Code:
    $ a.exe
    Current date and time is 31/12/2004 23:59:59
    timeUpdate
    Updated date and time is 31/12/2004 23:59:59
    
    Current date and time is 28/02/2008 23:59:58
    timeUpdate
    Updated date and time is 28/02/2008 23:59:58
    
    Current date and time is 28/02/2008 23:59:58
    timeUpdate
    Updated date and time is 28/02/2008 23:59:58
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by cougarsmustangs
    Thank you! it worked. What's that error showing in the main function? (see the compiler error - last line)
    You are learning from a book. How old is that book?

    In 1989, a standard for C was published with changed a few things for the better. Before that, the only real documentation for C was the book, "The C Programming Language" (1978), by Kernighan and Ritchie. Ritchie was the designer and implementer of C and that book served as the de facto standard, which became known as K&R after the authors' initials. After the 1989 ANSI standardization, they wrote a second edition of the book which is still widely available. Thus we had two versions of the language, K&R C and ANSI C, with K&R C being phased out. Turbo C had a setting for K&R, but most, if not all, modern compilers no longer accept it. This can cause problems for beginning programmers who search for source code on the Web and find old K&R code.

    Here is the cause of that error:
    Code:
    int main (void)
    
        int dt1;
        int dt2;
    
    {
    In K&R, that would have been the manner in which two int parameters would have been declared, though the void would have been wrong as I recall about K&R. I can see now that you had misplaced two global variables, but to the compiler their placement looked like an old K&R function definition.

    Try this instead:
    Code:
    int dt1;
    int dt2;
    
    int main (void)
    {
    That is, of course, assuming that you were trying to make those two variables global.

    PS

    A few examples to illustrate this major difference between K&R C and ANSI C. These are taken from some code that was written in 1987. I will only display the function definitions and none of the code they contain:
    Code:
    int altaz( pol, J )
    double pol[3];
    double J;
    {
    }
    
    int angles( p, q, e )
    double p[], q[], e[];
    {
    }
    As you can see, the function's parameter list only contains the names of the parameters, thus establishing their order in the list, but then their actual type declarations are given separately and (and this is very important) before the opening brace of the function's body.

    This is why you got that error message. When a compiler sees something placed between the function header and the opening brace, then it assumes that you're using K&R syntax, which is no longer supported.

    Of course, those two function implementations should in ANSI C be:
    Code:
    int altaz(double pol[3], double J )
    {
    }
    
    int angles(double p[], double q[], double e[])
    {
    }
    Even though you will never write K&R code, you should remember this for when you encounter older code that uses that syntax.

    Comments on this post

    • mythos_ agrees
    Last edited by dwise1_aol; March 24th, 2013 at 06:54 PM.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by cougarsmustangs
    This is what i got when i complied. it it didn't update??
    and where exactly do u think it should update? Did u write any line of code that updates the date/time?

IMN logo majestic logo threadwatch logo seochat tools logo