military time to integer conversion

This is a discussion on military time to integer conversion within the C Programming forums, part of the General Programming Boards category; good day sir's and maam, need help about my program. I want to create a program that will enter the ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    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. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    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?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Each time interval could simply be an integer: hours, minutes, seconds.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    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. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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
     }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  3. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  5. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21