# Thread: military time to integer conversion

1. ## military time to integer conversion

good day sir's and maam, need help about my program.
I want to create a program that will enter the time in and time out of an employee in military time(e.g. 08:00 - 17:00). from monday to friday. the problem sir, ma'am is how can i convert this to integer so that i can compute the total number of hours the employee work for the whole week.
thank you sir, maam for your time to read this and i would gladly appreciate any help from you.
Again thank you sir, maam. and God bless......

2. Couldn't really understand what's that difficult about it. Are you going to post your current code, or you're asking someone else to present you a complete solution?

3. Each time interval could simply be an integer: hours, minutes, seconds.

4. You need to do what's called 'parsing' the string. You could either use gets(), strtok(), and then atoi(), or you could simply use scanf() with a format string. A lot of it depends on how much you know about the format of the input. Are there ever spaces? Is there always a ':'. Are there always exactly two sets of two digits?

5. Once you've solved your parsing issues, you can represent the times as integeter minutes.

HH * 60 + MM

Then after you calculate the employee's daily work time and sum them for the entire week you can convert back to hours and fractional hours by dividing the total minutes by 60.

6. Code:
```this is my program, the input for time in and time out is string because of the colon e.g. 08:00, and if possible i also want to store the time in and time out in the file day.txt ex. 08:00,17:00,17:30,17:30,08:03,17:00,17:30,17:30,08:00,12:00,17:30,17:30,07:45,17:05,17:30,17:30,07:59,17:20,17:30,17:30
pls. kindly check my work. tnx

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fptr;
char x[5][9]={"Monday","Tuesday","Wednesday","Thursday","Friday"};
int i,y,a[5],b[5],total=0,total1=0;
char a1[5],b1[5];

fptr =fopen("day.txt","a");
clrscr();
for(i=0;i<5;i++)
{
printf("Enter time-in for %s: ",x[i]);
gets(a1);
printf("Enter time-out for %s: ",x[i]);
gets(b1);
fprintf(fptr,"%6s %6s",a1,b1);
fclose(fptr);
}
for(i=0;i<5;i++)
{
a[i] = atoi(a1[i]);
b[i] = atoi(b1[i]);
total = (b[i]-1) - a[i];
total1 += total;

}
printf("total no. of hrs for one week: %d",total1);
getch();
}```

7. Code:
```#include<stdio.h>
#include<stdlib.h>
main()// should return int
{
FILE *fptr;
char x[5][9]={"Monday","Tuesday","Wednesday","Thursday","Friday"};
int i,y,a[5],b[5],total=0,total1=0;
char a1[5],b1[5];

fptr =fopen("day.txt","a");
clrscr();
for(i=0;i<5;i++)
{
printf("Enter time-in for %s: ",x[i]);
gets(a1); // Never use gets.
printf("Enter time-out for %s: ",x[i]);
gets(b1);
fprintf(fptr,"%6s %6s",a1,b1);
fclose(fptr);
}
for(i=0;i<5;i++)
{
a[i] = atoi(a1[i]); // string.h header should have been included
b[i] = atoi(b1[i]);
total = (b[i]-1) - a[i];
total1 += total;

}
printf("total no. of hrs for one week: %d",total1);
getch(); // conio.h header should have been included
}```