Military Time Functions

This is a discussion on Military Time Functions within the C Programming forums, part of the General Programming Boards category; My program's purpose is to add two time intervals (starting time and duration, respectively...these are the input values the user ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Lightbulb Military Time Functions

    My program's purpose is to add two time intervals (starting time and duration, respectively...these are the input values the user must enter) and display the result.

    According to my professor, the error is in the GetEnding function. endingtime=startingtime+durationtime-60+100 is not a proper expression. It is true, because even 2505 (the time is supposed to be measured in military hours) is displayed as output, when it should read 0105 instead!

    What can I do to fix this problem?
    Code:
    /*Project #34
    Herbert Ortiz
    Due: September 27, 2004
    This programs adds two time intervals and displays the result*/
    
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    
    typedef enum Bool {false=0, true} bool;  //enable boolean functions
    
    int GetStarting(int startingtime);  //starting time function prototype
    int GetDuration(int durationtime);  //duration time function prototype
    bool IsTimeValid(int startingtime);  //validate starting time
    bool IsDTimeValid(int durationtime);  //validate duration time
    int GetEnding(int startingtime, int durationtime, int endingtime); //obtain the output
    
    main()
    {
     int startingtime, durationtime, endingtime;  //local variable declaration
    
     startingtime=GetStarting(startingtime);  //function call for startingtime input
    
     durationtime=GetDuration(durationtime); //function call for durationtime input
    
     if(IsTimeValid(startingtime)&&IsDTimeValid(durationtime)) //the below will be executed if...
     {
      endingtime=GetEnding(startingtime, durationtime, endingtime); //then this will be carried out
     }
    
     getch();
    }
    int GetStarting(int startingtime)   //function header/definition for starting time
    {
     printf("Please enter the time (military time): ");  //input prompt 1
     scanf("%d", &startingtime);   //variable evaluated and stored in memory
    
     return startingtime;  //value will return to main() for display
    }
    int GetDuration(int durationtime)  //function header/definition for duration time
    {
     printf("Please enter the duration: "); //input prompt 2
     scanf("%d", &durationtime);  //variable evaluated and stored in memory
    
     return durationtime;  //durationtime value will returns to main() for display
    }
    bool IsTimeValid(int startingtime)  //validation function for starting time
    {
     if(startingtime<2400)  //the condition for which the time is acceptable
     {
      return true;
     }
     else
     {
      return false;  //otherwise, the input is not acceptable
     }
    }
    bool IsDTimeValid(int durationtime)  //validation function for duration time
    {
     if(durationtime<2400)  //condition for which the time is acceptable
     {
      return true;
     }
     else
     {
      return false;  //if not, the input is invalid
     }
    }
    int GetEnding(int startingtime, int durationtime, int endingtime) //the end time function
    {
     endingtime=startingtime+durationtime-60+100;  //the mathematical procedure
    
     printf("Start time is %d. Duration is %d. End time is %d", startingtime, durationtime, endingtime);
     scanf("%d", &endingtime);    //output and its evaluation
    
     getch();
    }
    Thanks a lot for your time!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Define "proper expression". It's perfectly legal to do that. It may not do what you want it to, but it's perfectly legal in C. (Although I'm not sure why you're bothering to pass it as an argument, since you don't care what the value passed is, and since it doesn't actually return the value.)

    You'll have another problem, because when you call scanf inside that function, it's absolutely worthless, because the variable isn't a pointer, so all changes are lost when the function returns.

    As to the expression, you can simplify it to:

    endingtime = startingtime + durationtime + 40;

    Because N-60+100 is the same as saying N+40.

    Though again, if you're actually trying to read something into endingtime first, that statement is pointless.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Smile Oh, I see

    Yeah, I guess it's fine. The only thing is, I want 0000 instead of 2400 when I add, say, 1600 and 800. It has to conform with military time. Oh, what I meant by "proper expression" was simply that...If I get 2530 as a result of the sum of two input values startingtime and durationtime, then that is not an acceptable value. I wonder what I could do in order for that 2400 to revert back to 0000.

    As for the simplification, that does look much better! Thank you.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    why not just check to see if the value you get (endingtime) is above or equals 2400, and if so, decrease the value with 2400?

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Sounds Good

    So, something like

    Code:
    if(endingtime>=2400)
    {
     endingtime=endingtime-2400;
    }
    that would be inside the GetEnding() function...

    and then I have the endingtime=startingtime+durationtime-40
    and the printf() statement inside the else clause, right?

    That sounds like the tourniquet to my program's cut.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    You could replace that with:

    endingtime %= 2400;
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I have been waiting to see if anyone really knows what military time is. I personally feel that it's important to understand the problem before you start writing code.

    It isn't an integer that starts at 0000 and goes to ????

    Military time expresses hours from 00 to 23 and minutes from 00 to 59, and writes them together so that it looks like a 4-digit integer.

    For example suppose the present time is 12:45 pm in "human" time.

    In military time that's 1245, OK?

    Now suppose the duration is two hours and 30 minutes.

    What's the ending time? That is, what integer do you add to 1245 to get the ending time?

    Work another problem: suppose the starting time is 12:45 pm (1245 in military time). Suppose the ending time is 4:05 pm (same day). That's 1605 military. What's the duration? You can't just subtract 1245 from 1605. (Well you could, but you'd get the wrong answer.)

    Minutes: modulo 60
    Hours : modulo 24.



    As far as "proper expression", the following is a syntactically correct expression for calculating something, but does not give the correct answer for all possible input values. Whether it is "proper" or not, it doesn't solve the problem that was stated.

    Saying something is "not proper" sounds like an etiquette lesson. I would have said "wrong".

    Code:
    endingtime=startingtime+durationtime-60+100;
    Regards,

    Dave
    Last edited by Dave Evans; 10-10-2004 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. Elevator Function confusion
    By applescruff in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2004, 10:14 AM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 02:21 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21