extracting info from strdate

This is a discussion on extracting info from strdate within the C++ Programming forums, part of the General Programming Boards category; // The program below gives the output "10/010/01" //But I only want the dd of mm/dd/yy. //How do I extract ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    extracting info from strdate

    // The program below gives the output "10/010/01"
    //But I only want the dd of mm/dd/yy.
    //How do I extract just the day? Thanks.



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

    int main()
    {

    char datebuf[9];
    char day[2];
    char *digit1=&datebuf[3];
    char *digit2=&datebuf[4];

    _strdate(datebuf);
    strcpy(day,digit1);
    strcat(day,digit2);
    cout<<day<<endl;
    return 0;



    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > char day[2];
    You need at least 3 chars to store "dd\0" - gotta remember that \0 when dealing with 'C' style strings

    > char *digit1=&datebuf[3];
    > char *digit2=&datebuf[4];
    Ok, but unnecessary...

    > strcpy(day,digit1);
    > strcat(day,digit2);
    This is where the problem is - using strcpy and strcat to copy single chars
    Try
    Code:
    day[0] = datebuff[3];
    day[1] = datebuff[4];
    day[2] = '\0';
    Or if you insist on using a str... function
    Code:
    strncpy( day, &datebuff[3], 2 );
    day[2] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    Smile

    Thanks so much, Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. Extracting info from a packet trace file
    By Ho ming in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:54 AM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 02:12 PM
  5. Extracting info from string
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-25-2003, 09:04 AM

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