Solution: Fix the Code

This lesson provides solution to a challenge given in the previous lesson.

We'll cover the following...

Solution #

Here is the fixed code, that will produce the desired output upon entering 06:09 15:2 as input.

Press + to interact
import std.stdio;
import std.string;
import std.exception;
/* Reads the time as hour and minute after printing a
* message. */
void readTime(string message,
out int hour,
out int minute) {
write(message, "? (HH:MM) ");
readf(" %s:%s", &hour, &minute);
enforce((hour >= 0) && (hour <= 23) &&
(minute >= 0) && (minute <= 59),
"Invalid time!");
}
/* Returns the time in string format. */
string timeToString(int hour, int minute) {
assert((hour >= 0) && (hour <= 23));
assert((minute >= 0) && (minute <= 59));
return format("%02s:%02s", hour, minute);
}
/* Adds duration to start time and returns the result as the
* third pair of parameters. */
void addDuration(int startHour, int startMinute,
int durationHour, int durationMinute,
out int resultHour, out int resultMinute) {
resultHour = startHour + durationHour;
resultMinute = startMinute + durationMinute;
if (resultMinute > 59) {
++resultHour;
}
}
void main() {
int startHour;
int startMinute;
readTime("Start time", startHour, startMinute);
int durationHour;
int durationMinute;
readTime("Duration", durationHour, durationMinute);
int endHour;
int endMinute;
addDuration(startHour, startMinute,
durationHour, durationMinute,
endHour, endMinute);
writefln("%s hours and %s minutes after %s is %s.",
durationHour, durationMinute,
timeToString(startHour, startMinute),
timeToString(endHour, endMinute));
}

Enter the input below

Solution explanation

...