I am currently working on a program that calculates the total time needed to finish English and Mathematics homework. However, it freezes when I finish inputting. Please help.
SAMPLE INPUT 1:
66
99
SAMPLE OUTPUT 1:
Needed time to finish all homework: 2 hours 45 minutes
SAMPLE INPUT 2:
33
3
SAMPLE OUTPUT 2:
Needed time to finish all homework: 0 hour 36 minutes
#include<cstdio>
#include<iostream>
usingnamespace std;
int main(){
printf("Homework Time Calculator\n");
int eng,math,time,hour=0;
printf("Format: xx minutes\n");
printf("English> ");
scanf("%d",&eng);
printf("Mathematics> ");
scanf("%d",&math);
time=eng+math;
while(time>=60){
hour++;
}
if(hour>1) printf("Needed time to finish all homework: %d hours %d minutes\n",hour,time);
else printf("Needed time to finish all homework: %d hour %d minutes\n",hour,time);
system("pause");
return 0;
}
This is an infinite loop as there is nothing in the while loop to modify the value of time, so if time is >= 60 before the loop, the loop will run forever. Solution - Modify the value of time in the loop (time = time - 60;).