I am trying to write a code that calculates the end time of an event (in military time) based on 6 values the user enters: the starting hours, minutes, and seconds, and the duration hours, minutes and seconds.

The user can't enter values greater than 24, 60, and 60 for hours, minutes, and seconds, respectively, and they can't enter a negative number either. That part of the code I have working fine, but I'm having trouble computing the end time of the event. I can get it to work in some cases, but sometimes I get a negative value for seconds.

This is what I have:

// Compute ending time.

	endHour = startHours + durationHours ;
	endMinutes = startMinutes + durationMinutes ;
	endSeconds = startSeconds + durationSeconds ;

	// Convert seconds to minutes (etc.) or convert hours if it runs into the next day

	while (endHour >= 24 || endMinutes >= 60 || endSeconds >= 60)
	{
		if (endSeconds >= 60)
			endMinutes = endMinutes + 1;
			endSeconds = endSeconds - 60;
		if (endMinutes >= 60)
			endHour = endHour + 1;
			endMinutes = endMinutes - 60 ;
		if (endHour >= 24)
			endHour = endHour - 24;

	}

I was thinking of doing another while loop for if it comes out negative, but I'm not sure if there's some other general problem or if there's a more efficient way to go about it. Any help would be appreciated! :)

I don't see a problem. Maybe you need to post some details about the "sometimes".

By the way, I would get rid of the while and change the if statements into while statements. Do all the seconds at once then move on to the minutes.

[edit]
Oh.
You always subtract 60 from the seconds. You did not put the statements in a block using { and }

I don't see a problem. Maybe you need to post some details about the "sometimes".

By the way, I would get rid of the while and change the if statements into while statements. Do all the seconds at once then move on to the minutes.

[edit]
Oh.
You always subtract 60 from the seconds. You did not put the statements in a block using { and }

I put the statements in a block like you said, but now my seconds don't display at all. I input 12:30:00 as the start time and 12:34:33 as the duration, and I get:
1:4:

Same with trying the while loops instead of if statements. The hours and minutes are right, but I honestly have no clue what's going on with the seconds...before I put in the {} I was getting -27 seconds with those values.

How about calculating the end in seconds and then converting to h:m:s?

[edit]As for the formatting, I prefer printf, but you can produce zero-padding and fixed width other ways.

Then you did it wrong.

How about calculating the end in seconds and then converting to h:m:s?

Hmm, I was going to do that, but our instructions said not to convert to seconds.

Ah well, I think I'm just going to have to keep tinkering with it and maybe try something different. Thanks for your help, everyone.

Edit:
Fixed the blocked loops and I get the seconds right! So I did do it wrong. All fixed, thanks!

Hmm, I was going to do that, but our instructions said not to convert to seconds.

Ah well, I think I'm just going to have to keep tinkering with it and maybe try something different. Thanks for your help, everyone.

Edit:
Fixed the blocked loops and I get the seconds right! So I did do it wrong. All fixed, thanks!

Care to share?

Care to share?

I made a silly error and had one of my outside brackets in the wrong place. :$ This is why I am a beginner...

I meant the code...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.