Yes. That is better. I rewrit the prog, so it now looks like:
Code:
#include <stdio.h>
#include <conio.h>
/* MIKE GRUNDEL */
/*TASK 5: JOURNEY TIMES*/
int hours, minutes;
int main()
{
void INPUT_TIME();
//-------------------- DECLARE VARIABLES --------------------------------------//
int departure_hours, departure_minutes;
int arrival_hours, arrival_minutes;
int duration_hours, duration_minutes;
//---------------------- MAIN PROGRAM -----------------------------------------//
printf("--- CALCULATIONS BASED ON A 24 HOUR CLOCK ---\n");
//Enter departure time
printf("Enter the departure time of your train: ");
INPUT_TIME(departure_hours, departure_minutes);
departure_hours = hours;
departure_minutes = minutes;
//Enter arrival time
printf("Enter the arrival time of your train: ");
INPUT_TIME(arrival_hours, arrival_minutes);
arrival_hours = hours;
arrival_minutes = minutes;
//Calculate journey time
duration_hours = arrival_hours - departure_hours;
duration_minutes = arrival_minutes - departure_minutes;
//If there is going to be a negative number of minutes
if (duration_minutes < 0)
{
duration_minutes = 60 + duration_minutes; //invert minutes on a range of 60
arrival_hours = arrival_hours - 1; //the journey will now take one less hour
}
duration_hours = arrival_hours - departure_hours;
//If the journey travels through midnight
if (duration_hours < 0)
duration_hours = 24 + duration_hours; //invert hours on a scale of 24
//Display journey time
printf("The journey will take %i hours and %i minutes", duration_hours, duration_minutes);
getchar();
getchar();
return 0;
}
//-----------------------FUNCTIONS----------------------------//
void INPUT_TIME()
{
scanf("%i: %i", &hours, &minutes);
while (minutes > 59 || minutes < 0 || hours > 24 || hours < 0 || (hours == 24 && minutes > 0))
{
printf("Invalid. Re-enter the time: ");
scanf("%i:%i", &hours, &minutes);
}
}
One problem is that I dont know how to return variables from a void functon. I did it once before and I remember that you needed to use a * pointer thingy. It would be cool if someone could give me an example of how this program could be run without having to have "hours" and "minutes" as globals.
Another problem with this prog is if the time is not entered in HH:MM format, with the colon, the while loop for the validation runs infinitely. How could I stop that from happening?