time input help

This is a discussion on time input help within the C++ Programming forums, part of the General Programming Boards category; hi guys, I am making a program in which I have to enter start date and time and stop date ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Angry time input help

    hi guys,

    I am making a program in which I have to enter start date and time and stop date and time in format like 01:05:07 & 15:26:57

    i don't know how to get that value from the user in hh:mm:ss format and calculate the difference between different inputs. i have no clue of that. could you guys give me some tips on that?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are no direct functions to handle time in that way in the standard C & C++ languages. Although, I suppose difftime() could be used for calculating the differenc, if you wish.

    Reading time would be easiest to do by reading a string and then splitting it, I would think, but you could also create a time-class and specialized << and >> operators if you wish [which is made much easier if you write a function that takes a string and makes it into your internal format and from internal to string - then you can just use those string to/from functions to perform the input/output functions].

    The internal format of your time can be either h, m, s or seconds since midnight or some such - the latter is obviuously easier for math, but requires a bit more effort to format the output [not a whole lot tho].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    please do not take my questions as irritation as i am beginner

    you said that there are no functions to handle time this way but can i still take time from the user in hh:mm:ss format and then i can use difftime() function or the other solution that you have specified? if yes, can you give me one example of that so that it can be clear to me.

    thanks

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, difftime requires a full set of year, month, day, hour, minute and second. It is probably more complicated to get that set up than it is to calculate the time difference from hour, minute and second itself. I would suggest that you use a internal format of "seconds since midnight" and then calculate back hour, minute and second from that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    could you tell me how can i make system time refreshing every second?

    this is the code that displays the system time

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <io.h>
    #include <conio.h>
    
    using namespace std;
    
    int main(void)
    {
       FILE *stream;
       std::ftime ft;
    
       if ((stream = fopen("TEST.$$$","wt")) == NULL)
       {
          fprintf(stderr, "Cannot open output file.\n");
          return 1;
       }
       getftime(fileno(stream), &ft);
       cout<<"File time: "<<ft.ft_hour<<":"<<ft.ft_min<<":"<<ft.ft_tsec * 2;
       cout<<endl<<"File date: "<<ft.ft_month<<":"<<ft.ft_day<<":"<<ft.ft_year+1980;
       fclose(stream);
       getch();
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  4. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 04:41 AM
  5. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM

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