Thread: What else do I need in this simple time program...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    What else do I need in this simple time program...

    I'm trying to get a program to display time elapsed...what else do I need here?
    Also, I have times to be used as test vectors: time1 3:45:15 and time2 9:44:03....using military time

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    elapsed_time (time1, time2)
    {
    
    
        int time1;
        int time2;
        int hour;
        int min;
        int sec;
    };
    
    
    int main ()
    {
        elapsed_time (time1, time2)
    
    
    
    
        printf("Enter the time right now (hh  mm  ss): ");
        scanf("%i%i%i", &time1.hour, &time1.min, &time1.sec);
    
    
    
    
    }
    Last edited by Dom; 10-10-2011 at 11:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is elapsed_time supposed to be a struct or a function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    elapsed_time (time1, time 2) is the call so it probably is a function

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dom
    elapsed_time (time1, time 2) is the call so it probably is a function
    You are writing the code, so there is no "probably". It is, or it is not. If it is, then your function definition is missing the return type and the parameter types. Your main function is missing some variable declarations, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    Obviously......

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If it was obvious to you, then why did you ask?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    I'm not worrying about syntax right now or missing returns... I'm looking for other functions I would need to implement.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dom
    I'm not worrying about syntax right now or missing returns... I'm looking for other functions I would need to implement.
    In that case, I really don't see what is the problem: you need to record (or in this case, have the user input) the start time, end time, then compute the difference and display it. In terms of algorithm, not implementation, it is as simple as that.

    If you want to discuss an algorithm, I suggest that you do so in words, or by pseudocode, or with a flowchart (but this can be inconvenient here), rather than with a broken C program (unless you make it clear that it is not C, but pseudocode).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Alrighty...can you help me with my pseudo code?

    so I'll have

    Code:
    int main()
    {
         elapsed_time(time1, time2)  //to call function
    
         printf("Time elapsed from %i and %i = %i", time1, time2, ?)  
    
    // ? should i put difference of time1 and time2...i don't know how to subtract times because there are 60 seconds in a minute and not 100.
    
    }
    
    struct time
    {
         int hour;
         int minute;
         int second;
    }
    
    elapsed_time(time1, time2)
    {
    
    //what do I put in here, this?
    
    time1=3:45:15
    time2=9:44:03
    
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Dunno, you seem to be trying to implement, then you insist that you are just determining the steps of the algorithm.

    I have already told you what I think are the basic steps; I don't care if you have no clue as to how to represent the time and how to subtract the time, because that is just implementation detail. What I can say is that your question does not make sense: elapsed time means the difference between some start and end time, so asking if you should "put difference of time1 and time2" after printing the elapsed time is strange.

    On the other hand, if you are thinking of the implementation, I suggest that you check if the functionality available via <time.h> would be sufficient for your needs.
    Last edited by laserlight; 10-10-2011 at 12:49 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time program showing time since epoch?
    By cus in forum Linux Programming
    Replies: 5
    Last Post: 01-10-2009, 01:56 PM
  2. Simple program closing at end, no time to read...
    By Jake.c in forum C Programming
    Replies: 8
    Last Post: 09-08-2008, 02:20 PM
  3. Replies: 2
    Last Post: 05-09-2007, 08:46 AM
  4. Simple Text Parsing.. Long Time Since College
    By chops11 in forum C Programming
    Replies: 4
    Last Post: 10-07-2004, 04:00 PM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM