I'm Having Problem converting 24 hour into minutes

the given entry time is 0830
the given exits time is 1300
i'm using turbo c++
i know using normal calculations
i'm supposed to get 4 hours and 30 minutes
but i'm cracking my head up
trying to figure out how to do that
this is my coding so far

void main (void)
{

int entime,extime,total;
int h,m;
char C;


printf("METROPOLITAN PARKING SYSTEM\n");

printf("Enter Code: ");
scanf("%c",&C);

printf("Enter Entry Time: ");
scanf("%d",&entime);

printf("Enter Exits Time: ");
scanf("%d",&extime);

total = extime - entime ;

h = total / 100 ;

m = total % 100 ;

printf("Total Hour: %d hours %d minutes ",h,m);

}

This code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdio>
 
int main ()
{
 
int entime,extime,total;
int h,m;
char C;
 
 
printf("METROPOLITAN PARKING SYSTEM\n");
 
printf("Enter Code: ");
scanf("%c",&C);
 
printf("Enter Entry Time: ");
scanf("%d",&entime);
 
printf("Enter Exits Time: ");
scanf("%d",&extime);
 
total = extime - entime ;
 
h = total / 100 ;
 
m = total % 100 ;
 
printf("Total Hour: %d hours %d minutes ",h,m);
 
return 0;
 
}


with these inputs:

a
1200
1630



produces this output:

Total Hour: 4 hours 30 minutes


Using the inputs 0830 and 1300 gives 70 minutes.

This is because your code assumes there are 100 minutes in an hour (i.e. 1300 - 0830 = 470)
Last edited on
Yes you need to convert minutes.
1
2
3
4
5
6
extime = (extime / 100) * 100 + ( (extime % 100) * 100) / 60 );
entime = (entime / 100) * 100 + ( (entime % 100) * 100) / 60 );
total = extime - entime;

h = total / 100;
m = ( (total % 100) * 60) / 100;


It should work with this... i've assumed that entime > extime ...) I think you should also think about 2300 => 0300 ... and also about multiple days park ...
1
2
if (extime < entime) 
       extime+=2400;

this will resolve the 2300 => 0300 issue.
closed account (1vRz3TCk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdio>
 
int main ()
{
 
    int entime,extime,total;
    char C;
 
 
    printf("METROPOLITAN PARKING SYSTEM\n");
 
    printf("Enter Code: ");
    scanf("%c",&C);
 
    printf("Enter Entry Time: ");
    scanf("%d",&entime);
 
    printf("Enter Exits Time: ");
    scanf("%d",&extime);
 
    entime = ((entime / 100) *60) + ( entime % 100);
    extime = ((extime / 100) *60) + ( extime % 100);
    total = extime - entime ;
 
    printf("Total Hour: %d hours %d minutes ", (total / 60), (total % 60) );
 
    return 0;
 
}
Last edited on
thank you very much
you all are great help
thank you!!!

this is just phase 1 of my programming assignment
i'm so glad i've ask
thank you for replying
THANK YOU !!!!!

please assist me,if i face some more problem.
Topic archived. No new replies allowed.