checking military time

Apr 25, 2012 at 5:06pm
hey can someone help look over my code? I am trying to turn normal time into military time for a bigger program.
[void Beux::get_times()
{
cout<<"What hour did you depart on your frist day out?";
cin>>depart_hr;
cout<<"What minute was the flight supposed to leave?";
cin>>depart_minute;
while ((depart_minute>=60)||(depart_minute<0)) {
cout<<"Need legitimate minutes, try again."<<endl;
cin>>depart_minute;
}
cout<<"What your flight in the am or pm? (type a for am and p for pm)"<<endl;
cin>>depart_end;
depart_end=tolower(depart_end);
if ((depart_end=='p')&&((depart_hr<12)&&(depart_hr<0)))
{
depart_hr=depart_hr+12;
}
else if ((depart_hr==12)&&(depart_end=='a'))
{
depart_hr=0;
}
cout<<depart_hr;
cout<<"What time did you arive home? Enter Hours, then Minutes, then a for am or p for pm.";
cin>>arival_hr;
cin>>arival_minute;
cin>>arival_end;
arival_end=tolower(arival_end);
if ((arival_end=='p')&&((arival_hr<12)||(arival_hr<24))) {
arival_hr=arival_hr+12;
}
else if ((arival_hr==12)&&(arival_end=='a'))
{
arival_hr=0;
}
cout<<arival_hr<<endl;

}// - End times-][/code]

sorry I do not know how to use the code block ha
Apr 25, 2012 at 5:30pm
((depart_hr<12)&&(depart_hr<0)

If depart_hr is less than 12 AND it's less than zero? If it's less than zero, won't it always be less than 12? So why bother checking for less than 12? Did you really mean this?



Apr 25, 2012 at 5:49pm
it should be || for or but that isn't my problem, it is skipping the:

if ((depart_end=='p')&&((depart_hr<12)&&(depart_hr<0)))
{
depart_hr=depart_hr+12;
}
Apr 25, 2012 at 5:51pm
What test value of depart_end and depart_hr are you using? If depart_hr is zero or greater, then
1
2
3
{
depart_hr=depart_hr+12;
}
will always be skipped
Last edited on Apr 25, 2012 at 5:52pm
Apr 25, 2012 at 5:58pm
Just validate the user input for depart_hr to accept any integer between 0 and 23.

Then your check is much simpler:
1
2
3
4
if(depart_end =='p' && depart_hr < 12)
{
   depart_hr+=12;
}


Otherwise I think what you want is greater than zero:
1
2
3
4
if ((depart_end=='p')&&((depart_hr<12)&&(depart_hr>0)))
{
depart_hr=depart_hr+12;
} 
Last edited on Apr 25, 2012 at 5:59pm
Topic archived. No new replies allowed.