I am making a program that involves adding time.
It involves adding a beginning time, duration which would lead to the result of an end time from the sum of beginning time and duration. I will illustrate the problem i am having below:
12:15 start time with a duration of 45 minutes should lead to the end time being: 13:00
I wish to display all the times afterwards.

int beginHour = 12
int beginMinute = 15;
int durationHour = 0;
int durationMinute = 45;
int endHour;
int endMinute;

The outcome should look like:
Starttime: 12:15
Duration : 0:45
Endtime: 13:00

However it looks like this:
Startime: 13:15
Duration: 0:45
Endtime: 13:00

My code that is doing this:

if(beginHour + durationMinute >= 60)
{
   ++endHour, endMinute = (beginMinute + durationMinute)%60; 
   endHour = beginHour;
}

I'd appreciate if someone could show me where I am going wrong?

Recommended Answers

All 2 Replies

if(beginHour + durationMinute >= 60)

shouldnt beginHour be beginMinute? I assume the aim of this check is to see if adding on durationMinute to the time would take it past 60 min, ie need to increase the hour

edit: looking at the date I realise that you have probably worked this out...

The end hour has to be increased by one, you set endHour = beginHour;

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.