CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Posts
    2

    C++ Date and Time

    I need help please. I am new to C++. I want to create a program that would compute the weekly salary of the employees. The program would accept TIME-IN and TIME-OUT inputs then get the total hours for the day. The time format is in military time (e.g. 13:00 is 1:30 pm). I also want to get the total hours for the week.

    Inputs: (1234, Josef Alarka, 380.00, 08:00, 17:01, and June 11-15, 2009)

    Employee Number: 12345
    Name: Josef Alarka
    Salary Rate: 380.00
    Time-In for Monday: 08:00
    Time-Out for Monday: 17:01
    .
    .time in, time-out from monday to friday.
    .
    Time-In for Friday: 08:00
    Time-Out for Friday: 17:01
    Coverage Date: June 11-15, 2009

    Outputs:
    ************************
    Employee Number: 1234
    Name: Josef Alarka
    Salary Rate: Php 380.00 (with currency format. still i don't know how to format this)
    ************************
    Date Covered: June 11-15, 2009
    Total Number of Hours Worked: 40 hrs.
    Net Income: Php 1900.00
    ************************

    Any help is very much appreciated. Thank you very much.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ Date and Time

    Quote Originally Posted by josefalarka View Post
    The time format is in military time (e.g. 13:00 is 1:30 pm).
    1- You are confusing "Military Time" and "24h date format".
    2- 13:00 is NOT 1:30pm. it is 1:00pm. For example, 21:35 is 9h35pm.

    anyways, after this short rant, you should know that c++ has no built in time/date classes.
    You can either create your own (HIGHLY NOT RECOMMENDED), or look for a third party one. Boost probably has something, but I'm not sure.

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: C++ Date and Time

    boost has a large date-time lib. it can be a bit difficult to find what you want if its the first time using it.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++ Date and Time

    wxWidgets also has a nice date/time library.

    Actually, C++ does have a time structure... kind of. It's a C structure, so it's not recommended that you use in in your C++ code, but you can. It's called tm and it's in time.h

  5. #5
    Join Date
    Sep 2009
    Posts
    2

    Re: C++ Date and Time

    Still confused how to input the TIME-IN and TIME-OUT. My sample code below;

    #include<iostream.h>
    #include<time.h>
    int main()
    {
    time_t T_in;
    time_t T_out;
    time_t T_dif;

    cout<<"TIME-IN: ";
    cin>>T_in;
    cout<<"TIME-OUT: ";
    cin>>T_out;

    T_dif = T_out - T_in;

    cout<<"The total hours worked is: "<<T_dif<<endl;

    return 0;
    }

    OUTPUT:

    TIME-IN: 7:30
    TIME-OUT: The total hours worked is: -7

    I think the only value stored in T_in after I entered 7:30, is just 7. How to resolved this? Please help.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: C++ Date and Time

    You have to check the validity of the input. E.g.
    Code:
    if (cin >> T_in) {
        // valid input
    }
    else {
        // invalid input, need to flush input buffer
    }
    I'm not sure what the input format for time_t is.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured