My assignments requires me to display a starting time (hh:mm:ss) of user's input and travelling distance. The taxi is going in 90km/h. So ideally, I would want the program to display the time (15mins interval), from starting time to end time but in mm:ss format.
My approach is that I convert the starting time and end time to seconds, and I used a for loop. My interval would be i 900 (15mins in seconds). However, my for loop keeps running and never ends. Anyone can help me out ?
enter image description here <--- Expected output
My current code
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min minhour;
printf("Your starting time is d:d:d or d:d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
timesec = hour*3600 min*60 sec;
endtime = timesec ((distance/90)*3600);
printf("Starting time | Fare(%.1fkm)", distance);
for (int i=timesec; i<=endtime; i 900)
{
min = i/60;
sec = i - min;
printf("%d:%d | $XX.yy\n", min, sec);
}
CodePudding user response:
I removed this answer. Please accept other answer.
CodePudding user response:
Try this out:
int hour, min, sec, minhour, newmin, timesec, endtime; //ignore irrelevant variables
float distance, remdis, remdisb, firstfare, secondfare, fare;
printf("Input your starting travelling time in 24hrs clock (hh mm ss): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("Input your total travelling distance (km): ");
scanf("%f", &distance);
minhour = hour * 60;
newmin = min minhour;
printf("Your starting time is d:d:d or d:d (mm:ss)\n", hour, min, sec, newmin, sec);
printf("Your total travelling distance is %.1fkm or %.0fm\n", distance, distance*1000);
endtime = newmin ((distance*60)/90);
printf("Starting time | Fare(%.1fkm)\n", distance);
for (int i=newmin; i<=endtime; i =15)
{
hour = i/60;
min = i % 60;
printf("d:d | $XX.yy\n", hour, min);
}
CodePudding user response:
However, my for loop keeps running and never ends
for (int i=timesec; i<=endtime; i 900) ~~~~~~
Use i =900
, not i 900
here.