Thread: Structure and Function

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Question Structure and Function

    OK, each time I think I am starting to get it, I add something new, and get even more lost then before. I am trying to write a small program call a function that allows the user to enter a start time and stop time, then will calculate the difference and finally pass the results back to the main and print it from the main.

    Here is my current code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    	{
    	int hour;
    	int minute;
    	int second;
    	} TIME;
    
    int allTime ();
    
    int main (void)
    {
    
    allTime ();
    printf("\n\n\nThe total elapsed time is:  %2d hr(s),%2d min(s), and%2d sec(s)\n", totTime);
    
    return 0;
    }
    
    int allTime ()
    {
    
    TIME beginTime;
    TIME endTime;
    TIME totTime;
    
    printf("Calculate the total time between start and finish\n");
    printf("\nEnter the start time (hh:mm:ss) and press <enter>: ");
    scanf("%d:%d:%d", &beginTime.hour, &beginTime.minute, &beginTime.second);
    
    printf("\nEnter the finish time (hh:mm:ss) and press <enter>: ");
    scanf("%d:%d:%d", &endTime.hour, &endTime.minute, &endTime.second);
    
    totTime.hour = endTime.hour - beginTime.hour
    totTime.minute = endTime.minute - beginTime.minute;
    totTime.second = endTime.second - beginTime.second;
    
    return totTime;
    }
    There are a couple of things I am just not getting with this.

    1) How do I pass my solution back to the main, since this is where I want to print. Should I be trying to use pointers? I am a bit lost here.

    2) I can see an obvious flaw in the fact that if the user enters a start time of 5:00:01 and an End time of 3:00:01 the results will be negative. I am thinking if I can check to see if the hour is neg, and if it is, then add 12 to it, I should resolve this.

    3) How can I restict the user to be only able to put in 24 for hours, 60 for minutes and seconds. If not I would want to tell them they put in an illegal option and have them try again.


    Any direction, advice, assistance or guidance, as always will be greatly apprecaited.

    Thanks,
    DD
    "aut vincere aut mori"

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    did you try to compile this? try starting there

    Code:
    C:\WINDOWS\Desktop>gcc -c dum.c -o dum.o -Wall
    dum.c: In function `main':
    dum.c:17: `totTime' undeclared (first use in this function)
    dum.c:17: (Each undeclared identifier is reported only once
    dum.c:17: for each function it appears in.)
    dum.c:17: warning: too few arguments for format
    dum.c: In function `allTime':
    dum.c:37: parse error before `totTime'
    dum.c:40: incompatible types in return
    dum.c:41: warning: control reaches end of non-void function
    fix all that.
    Last edited by moi; 08-02-2002 at 02:24 PM.
    hello, internet!

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Two solutions:

    1st w/o pointers, remember to grab the return value and declare your functions with the correct return type:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	int hour;
    	int minute;
    	int second;
    } TIME;
    
    TIME allTime ();
    
    int main (void)
    {
       TIME totTime;
       totTime = allTime();
       printf("\n\n\nThe total elapsed time is:  %2d hr(s),%2d min(s), and%2d sec(s)\n", totTime);
       return 0;
    }
    
    TIME allTime ()
    {
       TIME beginTime;
       TIME endTime;
       TIME totTime;
    
       printf("Calculate the total time between start and finish\n");
       printf("\nEnter the start time (hh:mm:ss) and press <enter>: ");
       scanf("%d:%d:%d", &beginTime.hour, &beginTime.minute, &beginTime.second);
    
       printf("\nEnter the finish time (hh:mm:ss) and press <enter>: ");
       scanf("%d:%d:%d", &endTime.hour, &endTime.minute, &endTime.second);
    
       totTime.hour = endTime.hour - beginTime.hour;
       totTime.minute = endTime.minute - beginTime.minute;
       totTime.second = endTime.second - beginTime.second;
    
       return totTime;
    }
    2nd solution w/ pointers:
    Code:
    void allTime (TIME*);
    
    int main (void)
    {
       TIME tTime;
       allTime(&tTime);
       printf("\n\n\nThe total elapsed time is:  %2d hr(s),%2d min(s), and%2d sec(s)\n", tTime);
       return 0;
    }
    
    void allTime (TIME* totTime)
    {
       TIME beginTime;
       TIME endTime;
    
       printf("Calculate the total time between start and finish\n");
       printf("\nEnter the start time (hh:mm:ss) and press <enter>: ");
       scanf("%d:%d:%d", &beginTime.hour, &beginTime.minute, &beginTime.second);
    
       printf("\nEnter the finish time (hh:mm:ss) and press <enter>: ");
       scanf("%d:%d:%d", &endTime.hour, &endTime.minute, &endTime.second);
    
       totTime->hour = endTime.hour - beginTime.hour;
       totTime->minute = endTime.minute - beginTime.minute;
       totTime->second = endTime.second - beginTime.second;
    }

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    Good start.
    1) How do I pass my solution back to the main, since this is where I want to print. Should I be trying to use pointers? I am a bit lost here.
    First, your function allTime needs to return a value for you to put into totTime. Your function is of type int, but I recommend you change it to type TIME:
    Code:
    TIME allTime ();
    Then, make the function return a value to variable totTIme:
    Code:
    totTime = allTime ();
    See what that does.
    2) I can see an obvious flaw in the fact that if the user enters a start time of 5:00:01 and an End time of 3:00:01 the results will be negative. I am thinking if I can check to see if the hour is neg, and if it is, then add 12 to it, I should resolve this
    That's up to you. Try some things and have fun with it


    3) How can I restict the user to be only able to put in 24 for hours, 60 for minutes and seconds. If not I would want to tell them they put in an illegal option and have them try again.
    I think the answer to this is tied up with your solution to question two.

    Bon apetit.

  5. #5
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32
    Actually I get a lot less compile errors:

    they are


    C:\test.c(17) : error C2065: 'totTime' : undeclared identifier
    C:\test.c(37) : error C2146: syntax error : missing ';' before identifier 'totTime'
    C:\test.c(40) : error C2115: 'return' : incompatible types


    Which I figure has everything to do with my first question. How do I actually pass the results from the function back to the main to be printed.

    If I modify my code and put the printf statement that is in the main, into the function and change the functions return to 0, I can compile without error, link and run, then I just have my error checking issues (questions 2 and 3).


    Also note I forgot a semicolon in this line:
    Code:
    totTime.hour = endTime.hour - beginTime.hour;


    But I would like to actually pass the results of the function to the main for printing. So there is the major problem I am having.

    Thanks,
    DD
    "aut vincere aut mori"

  6. #6
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    OK I am slow

    LOL in th eprocess of typing I get more help, thanks guys, I will look this over.

    As always I do greatly appreciate the help.
    DD
    "aut vincere aut mori"

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by DocDroopy
    Actually I get a lot less compile errors:

    they are


    C:\test.c(17) : error C2065: 'totTime' : undeclared identifier
    C:\test.c(37) : error C2146: syntax error : missing ';' before identifier 'totTime'
    C:\test.c(40) : error C2115: 'return' : incompatible types


    Which I figure has everything to do with my first question. How do I actually pass the results from the function back to the main to be printed.

    If I modify my code and put the printf statement that is in the main, into the function and change the functions return to 0, I can compile without error, link and run, then I just have my error checking issues (questions 2 and 3).


    Also note I forgot a semicolon in this line:
    Code:
    totTime.hour = endTime.hour - beginTime.hour;


    But I would like to actually pass the results of the function to the main for printing. So there is the major problem I am having.

    Thanks,
    DD
    alright, so you get less errors. your compiler is inferior to gcc . still, fix the ones you have, particularly the parse/syntax error: why should we have to (in addition to actually helping you with your code) be figuring out where you forgot semicolons or put in an extra brace or whatever?
    hello, internet!

  8. #8
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Never meant for you to.

    Never meant for you to fix my syntax errors such as the semicolon, that was entirely my fault while editing the code before posting I got a little delete happy (sorry about that). Sorry if I did not understand what you were saying about the errors Moi. It was your comment that got me to re-read and see that, and I do appreciate that and corrected it in the post.

    But I did know there would be some general compile errors, it was because I was trying to pass an integer when I did not have one defined to pass, this is what was really messing me up and the basis to question one.

    Got that fixed now. Declared the function to be TIME as suggested, and then declared the variable TIME totTime; in the program so toTime would be passed and read correctly. Thanks everyone for the help. Now I just need to see if I can figure out the error checking.

    DD
    "aut vincere aut mori"

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    oh btw one more thing.

    going from the list of errors your compiler made, the second one indicates the stupid typo you made. but the first and third are integral to the main problems in your code, which cshot corrected for you. but maybe you might have been able to fix it yourself if you had tried to compile and read the errors?

    getting help is great, but i always get more satisfaction when i fix a problem myself rather than getting help for it, and i think most programmers are the same way.

    just trying to help a bit
    hello, internet!

  10. #10
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    could you do this too?

    [code]
    typedef struct
    {
    short hour;
    short minute;
    short second;
    } TIME;
    [\code]

    I believe you could also do this to save on the number of bytes you'll use with this program. Its fine the way it is really but I just wanted to throw that out there to see whatcha think bout that.
    P.S. You'll have to change your conversion factors too.
    cj

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM