-
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!
-
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.
-
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.
-
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?
-
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. :)
-
You could replace that with:
endingtime %= 2400;
-
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