problem with time

This is a discussion on problem with time within the C++ Programming forums, part of the General Programming Boards category; Hello all...I am still very new to programming. I need to output the date in this form ##/##/#### I have ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Question problem with time

    Hello all...I am still very new to programming. I need to output the date in this form ##/##/####

    I have tried to do my homework first, but I guess to be honest I just don't understand it well enought. I have <time.h> included.... and i found a reference that says the structure tm includes tm_day, tm_mon, tm_year...which I think is what I need but I don't know how to get it in my code. Can you help me please....I have read a ton of stuff...I can get it to output in string form, but not numerical form...i just need an example to follow.

    Thankyou so much,
    bigb1999

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You're over complicating it, validate the format of the input and then validate the values. Read the line from the user, verify that there are two integer values, a / character, two more integer values, another / character, and four more integers.

    Once you know the formatting is correct, then check the actual values of the integers. The first can't go higher than 12, the second can't go higher than 31, and the third can't go wherever you want your falloff year to be. This is all if..else statments and can be improved by testing what month it is and then checking that the day doesn't exceed the bounds of that month.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    I need it to read time from the system...not output the date that the user has entered. I need to use some structure of time, but I am not sure exactly how to get it in the format that I stated above. I can get it to output the system time in the string format...but not ##/##/####....
    anymore help would be great...
    bigb1999

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Get the time with time(), use gmtime() or localtime() to convert the system time into an instance of the struct tm structure. You can then use those values to format the output however you want. This page should help.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Question

    Thanks for all of your help.... this is what I have so far...
    I have been successful enough to get that far...but how do i get a (for example) 0 to preceed the 3, since it is the third month... or get a 0 to proceed the day of the month if it was under 10.... any direction there...

    #include <stdio.h>
    #include <time.h>
    #include <iostream.h>

    struct tm* timetm()
    {
    time_t now;
    time(&now);
    struct tm* today = localtime(&now);
    return today;
    }



    int main()
    {
    struct tm* now = timetm();
    char stime [64];
    sprintf (
    stime,
    "%d/%d/%d\n",
    now->tm_mon+1,
    now->tm_mday,
    now->tm_year+1900
    );

    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Fun with printf:
    Code:
    #include <cstdio>
    
    int main ( void )
    {
      int x = 1, y = 10;
      printf ( "Fun with printf: %0*d\n", ( x < 10 ) ? 2 : 1, x );
      printf ( "Fun with printf: %0*d\n", ( y < 10 ) ? 2 : 1, y );
      return 0;
    }
    -Prelude
    Last edited by Prelude; 03-24-2002 at 10:31 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 02:45 PM
  2. another problem with catstr() this time
    By the_winky_files in forum C Programming
    Replies: 19
    Last Post: 09-22-2005, 04:20 PM
  3. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM

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