Thread: Struct Problem

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Struct Problem

    Question: Write a function called clockKeeper that takes as its argument a dateAndTime structure. THe function should call the timeUpdate function, and if the time reaches midnight, the function should call the dateUpdate function to switch over to the next day. Have the function return the updated dateAndTime structure.

    My attempt: please see the attached file.


    Any help is much appreciated.

    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    my attempt: (but the compiler is showing many errors which i couldn't seem to figure out)

    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;
    
    
    }
    Last edited by cougarsmustangs; 03-23-2013 at 10:41 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by cougarsmustangs View Post
    my attempt: (but the compiler is showing many errors which i couldn't seem to figure out)
    It helps if you post them.

    Code:
    mingw32-gcc.exe -Wall  -g  -pedantic -Wextra -Wall -ansi -g    -c C:\Users\Josh2\Documents\sandboxc\main.c -o obj\Debug\main.o
    C:\Users\Josh2\Documents\sandboxc\main.c:6:54: error: parameter 1 ('dt') has incomplete type
    C:\Users\Josh2\Documents\sandboxc\main.c:6:21: error: return type is an incomplete type
    C:\Users\Josh2\Documents\sandboxc\main.c: In function 'clockKeeper':
    C:\Users\Josh2\Documents\sandboxc\main.c:12:3: error: type of formal parameter 1 is incomplete
    C:\Users\Josh2\Documents\sandboxc\main.c:17:5: error: type of formal parameter 1 is incomplete
    C:\Users\Josh2\Documents\sandboxc\main.c:20:3: warning: 'return' with a value, in function returning void [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:6:54: warning: unused parameter 'dt' [-Wunused-parameter]
    C:\Users\Josh2\Documents\sandboxc\main.c: In function 'main':
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: error: variable 'dt1' has initializer but incomplete type
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: error: extra brace group at end of initializer
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: error: (near initialization for 'dt1')
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: warning: excess elements in struct initializer [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: warning: (near initialization for 'dt1') [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: error: extra brace group at end of initializer
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: error: (near initialization for 'dt1')
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: warning: excess elements in struct initializer [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:28:10: warning: (near initialization for 'dt1') [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:28:23: error: storage size of 'dt1' isn't known
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: error: variable 'dt2' has initializer but incomplete type
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: error: extra brace group at end of initializer
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: error: (near initialization for 'dt2')
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: warning: excess elements in struct initializer [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: warning: (near initialization for 'dt2') [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: error: extra brace group at end of initializer
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: error: (near initialization for 'dt2')
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: warning: excess elements in struct initializer [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:29:10: warning: (near initialization for 'dt2') [enabled by default]
    C:\Users\Josh2\Documents\sandboxc\main.c:29:23: error: storage size of 'dt2' isn't known
    C:\Users\Josh2\Documents\sandboxc\main.c:49:11: error: expected ')' before 'dt2'
    C:\Users\Josh2\Documents\sandboxc\main.c:73:11: error: expected ')' before 'dt2'
    C:\Users\Josh2\Documents\sandboxc\main.c:29:23: warning: unused variable 'dt2' [-Wunused-variable]
    C:\Users\Josh2\Documents\sandboxc\main.c:28:23: warning: unused variable 'dt1' [-Wunused-variable]
    Process terminated with status 1 (0 minutes, 0 seconds)
    18 errors, 12 warnings (0 minutes, 0 seconds)
    Where did you write your structure declarations? Most of the rest are here because you didn't actually declare or include the structure types in this file.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    how about now?

    Code:
    #include <stdio.h>
    
    
    struct date
    {
    	int month;
    	int day;
    	int year;
    };
    
    
    struct time
    {
    	int seconds;
    	int minutes;
    	int hour;
    };
    
    
    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;
    
    
    }

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    I compiled it and its still showing errors:




    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'

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by cougarsmustangs View Post
    how about now?
    Better, but still missing type declarations.

    Quote Originally Posted by cougarsmustangs View Post
    I compiled it and its still showing errors:
    ex9.5.c:17:53: error: parameter 1 ('dt') has incomplete type

    Notice the type of dt
    Notice that type declaration is missing.
    Thus everything that depends on that declaration is "wrong".

    If you still have compiler errors after correcting that, then they should be easy to figure out.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    am i on the right track? I compiled this and its still showing pretty much the same errors
    Code:
    #include <stdio.h>
    
    
    int dt;
    
    
    struct date
    {
        int day;
        int month;
        int year;
    };
    
    
    struct time
    {
        int seconds;
        int minutes;
        int hour;
    };
    
    
    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)
    
    
    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;
    
    
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh oh me me me - I knows teh answers to this one -> Struct Problem Please help - Dev Shed
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A struct problem
    By Salahuddin in forum C Programming
    Replies: 8
    Last Post: 09-04-2011, 03:38 AM
  2. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  3. struct problem
    By spudval in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2007, 04:22 PM
  4. Struct problem
    By chr15 in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 02:37 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM