I am trying to parse a date stored as an array of characters. I have a problem to paerse the date with a loop otherwise it works fine.

char date[]="20141018"; //the date is 10/18/2014
char _mypointer=date;

char year= "YYYY";
char month="MM";
char day= "DD";

year[0]=*my_pointer + 0;
month[1]=*my_pointer +1;

What I want is the loop to do this but I am getting serios errors:

int i,j =0;

do 

    i++;
    j++;} while(i<4 and j<4);

Recommended Answers

All 8 Replies

Would you care to mention, WHAT those serious errors are?

char year= "YYYY";

You're trying to create a char object here.A char object can hold ONE char. One. You're trying to somehow store "YYYY" in an object that can hold ONE char only.

year[] = "YYYY"; would do what you want. Likewise with month and day.

You may have other errors ... best to post the whole section (or program).

/*
char year= "YYYY";
char month="MM";
char day= "DD";
*/

/*  But ... I suspect you want to have: */

const char* year= "YYYY";
const char* month="MM";
const char* day= "DD";

Also, these lines are wrong

char date[]="20141018"; //the date is 10/18/2014
char _mypointer=date;

they should be

char date[]="20141018"; //the date is 10/18/2014
char* _mypointer=date;

Also, DO NOT use leading underscores for local variables. That construct is generally reserved for system libraries and such.

There are other errors as well. Look carefully at your code and see if you can detect what they are. Consider your use of pointer arithmetic. I don't think it is doing what you think it is.

Also, DO NOT use leading underscores for local variables. That construct is generally reserved for system libraries and such.

Only at file scope in normal and tag name spaces. A single leading underscore is safe for local variables provided the second character is not an upper case letter. Double leading underscores are always reserved though.

That said, best practice is to avoid leading underscores in general, unless you're very familiar with the rules of reserved identifiers.

Can you please explain in details what you are trying to do ?
First of all it's a erroneous code. So it's expected to get a lots of errors. I think proper explanation may give a clue to what problem you are facing actually.

I am trying to parse a date stored as an array of characters. I have a problem (to parse the date) with a loop ...

This may help you get started ... note:

  1. input loop used
  2. use of functions
  3. input is validated
  4. fgets is used for input ... (and fgets is 'fixed')

Oops ...having a problem keeping code format here ... see next page

/* parseOutDate.c */  /* 2014-03-03 */


/*
    I am trying to parse a date stored as an array of characters.
    I have a problem to parse the date with a loop ....
*/


/*  http://developers-heaven.net/forum/index.php/topic,2608.0.html */

#include <stdio.h>
#include <string.h> /* re. strlen, strncpy, strchr ... */
#include <ctype.h>  /* re. isdigit ... */

int more() /* defaults to 'yes'/'1' unless 'n' or 'N' entered */
{
    int reply;
    fputs( "More (y/n) ? ", stdout ); fflush( stdout );
    reply = getchar();
    if( reply != '\n' )
        while( getchar() != '\n' ); /* 'flush' stdin buffer */

    if( reply == 'n' || reply == 'N' ) return 0;
    /* else ... if reach here ... */
    return 1;
}

/* returns 'true' only if length is 8 and only all digits entered ... */
int validEntry( const char* s )
{
    int i,
        len = strlen(s);

    if( len != 8 )
    {
        printf( "\nExpecting 8 char's only ... try again.\n");
        return 0;
    }

    for( i = 0; i < len; ++i )
    if( !isdigit(s[i]) )
    {
        printf( "\nExpecting only digits ... try again.\n");
        return 0;
    }

    /* else ... if reach here ... */
    return 1;
}


int main()
{
    /* for entry "20141018" ... the date is 10/18/2014 */

    char year[4+1]; /* Recall, need 1 extra char for '\0' char at end */
    char month[2+1];
    char day[2+1];

    char buf[80];

    do
    {
        char* p;

        printf( "Enter a date (with entered format YYYYMMDD) :  " );
        fflush( stdin );
        fgets( buf, sizeof(buf), stdin );

        /* fix fgets ... */
        p = strchr( buf, '\n' );
        if( *p ) *p = 0; /* get rid of '\n' char at end ... */
        else while( getchar() != '\n' ) ; /*  'flush ' stdin ... */

        /* NOW can check ... */
        if( !validEntry( buf ) ) continue; /* jump to while 'check' */

        strncpy( year, buf, 4 );
        year[4] = 0;

        strncpy( month, buf+4, 2 );
        month[2] = 0;

        strncpy( day, buf+6, 2 );
        day[2] = 0;

        printf( "\nYou entered: %s\n", buf );
        printf( "i.e. %s-%s-%s\n", month, day, year );
    }
    while( more() );

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.