strcat problem

Discussion in 'C Programming' started by ctara_shafa@yahoo.co.uk, Jan 8, 2006.

  1. Guest

    Hi,
    I have a following problem: I'm creating a list and one of the fields
    should contain the date. Firstly I ask the user for the year, month and
    day and then I'd like to collect all this data in one field. To do this
    I tried to concatenate those 3 numbers into one using strcat. A piece
    of code is as follows

    /*function to enter the date*/
    char * date (void)
    {
    long year; /* it must of a long type */
    long month; /* because of some other reasons*/
    long day;
    char *str1;
    char *str2;
    char *str3;
    char *str4;

    /*here are the functions collecting the data from the input*/

    str1 = strcat (year,'-');
    str2 = strcat (str1, month);
    str3 = strcat (str2,'-');
    str4 = strcat (str3,day);

    return str4;
    }

    I think the result should be "year-month-day".. Why it doesn't work?
     
    , Jan 8, 2006
    #1
    1. Advertising

  2. Ico Guest

    wrote:

    > I have a following problem: I'm creating a list and one of the fields
    > should contain the date. Firstly I ask the user for the year, month and
    > day and then I'd like to collect all this data in one field. To do this
    > I tried to concatenate those 3 numbers into one using strcat. A piece
    > of code is as follows


    Posting your question 3 times in a row is *not* going to help you in
    getting friendly, helpful answers. If your newsreader is broken, use a
    better one. If you don't know how to use it properly, learn. If you
    posted 3 times on purpose, don't expect people to be nice to you.

    Ico

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Jan 9, 2006
    #2
    1. Advertising

  3. Guest

    No it does not work as you think

    wrote:
    > Hi,
    > I have a following problem: I'm creating a list and one of the fields
    > should contain the date. Firstly I ask the user for the year, month and
    > day and then I'd like to collect all this data in one field. To do this
    > I tried to concatenate those 3 numbers into one using strcat. A piece
    > of code is as follows
    >
    > /*function to enter the date*/
    > char * date (void)
    > {
    > long year; /* it must of a long type */
    > long month; /* because of some other reasons*/
    > long day;
    > char *str1;
    > char *str2;
    > char *str3;
    > char *str4;
    >
    > /*here are the functions collecting the data from the input*/
    >
    > str1 = strcat (year,'-');
    > str2 = strcat (str1, month);


    The Syntax of strcat is

    char *strcat(char *s1, const char *s2);

    The strcat() function appends a copy of the string pointed to by s2
    (including the terminating null character) to the end of the string
    pointed to by s1. The initial character of s2 overwrites the null
    character at the end of s1. If copying occurs between objects that
    overlap, the behavior is undefined.

    The function strcat() does not allocate any storage. The caller must
    insure that the buffer pointed to by s1 is long enough for string s2
    and its terminating null character.

    is year a char* ???

    > str3 = strcat (str2,'-');
    > str4 = strcat (str3,day);
    >


    Where is the memory allocation for str1, str2, str3 and str4 ???

    > return str4;
    > }
    >
    > I think the result should be "year-month-day".. Why it doesn't work?


    use sprintf to do that
    The below code would work as you thought

    char * date (void)
    {
    long year; /* it must of a long type */
    long month; /* because of some other reasons*/
    long day;

    char caBuffer[4]; /* used for conversion */

    char* str1;

    str1 = (char*) malloc (11);
    sprintf (caBuffer, "%lu", year);
    strcpy (str1, year);
    strcat ((str1,'-');
    sprintf (caBuffer, "%lu", month);
    strcat (str1, month);
    strcat (str1,'-');
    sprintf (caBuffer, "%lu", day);
    strcat (str1, day);

    return str1;
    }

    Best Regards,
    Abdur
     
    , Jan 9, 2006
    #3
  4. Guest

    wrote:
    > No it does not work as you think
    >
    > wrote:
    > > Hi,
    > > I have a following problem: I'm creating a list and one of the fields
    > > should contain the date. Firstly I ask the user for the year, month and
    > > day and then I'd like to collect all this data in one field. To do this
    > > I tried to concatenate those 3 numbers into one using strcat. A piece
    > > of code is as follows
    > >
    > > /*function to enter the date*/
    > > char * date (void)
    > > {
    > > long year; /* it must of a long type */
    > > long month; /* because of some other reasons*/
    > > long day;
    > > char *str1;
    > > char *str2;
    > > char *str3;
    > > char *str4;
    > >
    > > /*here are the functions collecting the data from the input*/
    > >
    > > str1 = strcat (year,'-');
    > > str2 = strcat (str1, month);

    >
    > The Syntax of strcat is
    >
    > char *strcat(char *s1, const char *s2);
    >
    > The strcat() function appends a copy of the string pointed to by s2
    > (including the terminating null character) to the end of the string
    > pointed to by s1. The initial character of s2 overwrites the null
    > character at the end of s1. If copying occurs between objects that
    > overlap, the behavior is undefined.
    >
    > The function strcat() does not allocate any storage. The caller must
    > insure that the buffer pointed to by s1 is long enough for string s2
    > and its terminating null character.
    >
    > is year a char* ???
    >
    > > str3 = strcat (str2,'-');
    > > str4 = strcat (str3,day);
    > >

    >
    > Where is the memory allocation for str1, str2, str3 and str4 ???
    >
    > > return str4;
    > > }
    > >
    > > I think the result should be "year-month-day".. Why it doesn't work?

    >
    > use sprintf to do that
    > The below code would work as you thought
    >
    > char * date (void)
    > {
    > long year; /* it must of a long type */
    > long month; /* because of some other reasons*/
    > long day;
    >
    > char caBuffer[4]; /* used for conversion */
    >
    > char* str1;
    >
    > str1 = (char*) malloc (11);
    > sprintf (caBuffer, "%lu", year);
    > strcpy (str1, year);
    > strcat ((str1,'-');
    > sprintf (caBuffer, "%lu", month);
    > strcat (str1, month);
    > strcat (str1,'-');
    > sprintf (caBuffer, "%lu", day);
    > strcat (str1, day);
    >
    > return str1;
    > }
    >
    > Best Regards,
    > Abdur


    I am sorry, a small correction in the programm

    char * date (void)
    {
    long year; /* it must of a long type */
    long month; /* because of some other reasons*/
    long day;

    char caBuffer[4]; /* used for conversion */

    char* str1;

    str1 = (char*) malloc (11);
    sprintf (caBuffer, "%lu", year);
    strcpy (str1, (caBuffer);
    strcat ((str1,'-');
    sprintf (caBuffer, "%lu", month);
    strcat (str1, (caBuffer);
    strcat (str1,'-');
    sprintf (caBuffer, "%lu", day);
    strcat (str1, (caBuffer);


    return str1;
    }

    Best Regards,
    Abdur
     
    , Jan 9, 2006
    #4
  5. Guest

    wrote:
    > No it does not work as you think
    >
    > wrote:
    > > Hi,
    > > I have a following problem: I'm creating a list and one of the fields
    > > should contain the date. Firstly I ask the user for the year, month and
    > > day and then I'd like to collect all this data in one field. To do this
    > > I tried to concatenate those 3 numbers into one using strcat. A piece
    > > of code is as follows
    > >
    > > /*function to enter the date*/
    > > char * date (void)
    > > {
    > > long year; /* it must of a long type */
    > > long month; /* because of some other reasons*/
    > > long day;
    > > char *str1;
    > > char *str2;
    > > char *str3;
    > > char *str4;
    > >
    > > /*here are the functions collecting the data from the input*/
    > >
    > > str1 = strcat (year,'-');
    > > str2 = strcat (str1, month);

    >
    > The Syntax of strcat is
    >
    > char *strcat(char *s1, const char *s2);
    >
    > The strcat() function appends a copy of the string pointed to by s2
    > (including the terminating null character) to the end of the string
    > pointed to by s1. The initial character of s2 overwrites the null
    > character at the end of s1. If copying occurs between objects that
    > overlap, the behavior is undefined.
    >
    > The function strcat() does not allocate any storage. The caller must
    > insure that the buffer pointed to by s1 is long enough for string s2
    > and its terminating null character.
    >
    > is year a char* ???
    >
    > > str3 = strcat (str2,'-');
    > > str4 = strcat (str3,day);
    > >

    >
    > Where is the memory allocation for str1, str2, str3 and str4 ???
    >
    > > return str4;
    > > }
    > >
    > > I think the result should be "year-month-day".. Why it doesn't work?

    >
    > use sprintf to do that
    > The below code would work as you thought
    >
    > char * date (void)
    > {
    > long year; /* it must of a long type */
    > long month; /* because of some other reasons*/
    > long day;
    >
    > char caBuffer[4]; /* used for conversion */
    >
    > char* str1;
    >
    > str1 = (char*) malloc (11);
    > sprintf (caBuffer, "%lu", year);
    > strcpy (str1, year);
    > strcat ((str1,'-');
    > sprintf (caBuffer, "%lu", month);
    > strcat (str1, month);
    > strcat (str1,'-');
    > sprintf (caBuffer, "%lu", day);
    > strcat (str1, day);
    >
    > return str1;
    > }
    >
    > Best Regards,
    > Abdur


    I am sorry, a small correction in the programm

    char * date (void)
    {
    long year; /* it must of a long type */
    long month; /* because of some other reasons*/
    long day;

    char caBuffer[4]; /* used for conversion */

    char* str1;

    str1 = (char*) malloc (11);
    sprintf (caBuffer, "%lu", year);
    strcpy (str1, caBuffer);
    strcat (str1,'-');
    sprintf (caBuffer, "%lu", month);
    strcat (str1, caBuffer);
    strcat (str1,'-');
    sprintf (caBuffer, "%lu", day);
    strcat (str1, caBuffer);


    return str1;
    }

    Best Regards,
    Abdur
     
    , Jan 9, 2006
    #5
  6. Ico Guest

    <> wrote:

    [..]

    > Best Regards,
    > Abdur


    Is it my reader that is broken, or is everybody posting twice or thrice
    today ?

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Jan 9, 2006
    #6
  7. Guest

    Ico wrote:
    > <> wrote:
    >
    > [..]
    >
    > > Best Regards,
    > > Abdur

    >
    > Is it my reader that is broken, or is everybody posting twice or thrice
    > today ?
    >
    > --
    > :wq
    > ^X^Cy^K^X^C^C^C^C


    I hope, it has happened by mistake. I am sorry about it

    Best Regards,
    Abdur
     
    , Jan 9, 2006
    #7
  8. M.B Guest

    wrote:
    > Hi,
    > I have a following problem: I'm creating a list and one of the fields
    > should contain the date. Firstly I ask the user for the year, month and
    > day and then I'd like to collect all this data in one field. To do this
    > I tried to concatenate those 3 numbers into one using strcat. A piece
    > of code is as follows
    >
    > /*function to enter the date*/
    > char * date (void)
    > {
    > long year; /* it must of a long type */
    > long month; /* because of some other reasons*/
    > long day;
    > char *str1;
    > char *str2;
    > char *str3;
    > char *str4;
    >
    > /*here are the functions collecting the data from the input*/
    >


    > str1 = strcat (year,'-');


    No memory allocated for str1,str2,str3,str4

    > str2 = strcat (str1, month);
    > str3 = strcat (str2,'-');
    > str4 = strcat (str3,day);
    >

    in any case syntax for strcat is
    char *strcat(char *,char *)
    '-' does not work here "-" does.

    > return str4;
    > }
    >
    > I think the result should be "year-month-day".. Why it doesn't work?
     
    M.B, Jan 9, 2006
    #8
  9. Ico <> writes:
    > <> wrote:
    >
    > [..]
    >
    >> Best Regards,
    >> Abdur

    >
    > Is it my reader that is broken, or is everybody posting twice or thrice
    > today ?


    I think groups.google.com is now broken in yet another way. Whatever
    the problem is, it's new (and presumably not the posters' fault).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 9, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Stanley

    strcat problem

    Ian Stanley, Sep 12, 2003, in forum: C Programming
    Replies:
    5
    Views:
    3,627
    Irrwahn Grausewitz
    Sep 13, 2003
  2. Ian Stanley

    strcat problem again

    Ian Stanley, Sep 17, 2003, in forum: C Programming
    Replies:
    18
    Views:
    708
    Martijn Lievaart
    Sep 22, 2003
  3. JC

    strcpy and strcat problem

    JC, Sep 26, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,946
    Robert B. Clark
    Sep 29, 2003
  4. Replies:
    24
    Views:
    1,232
    Greg Comeau
    Oct 30, 2005
  5. alternativa

    strcat problem

    alternativa, Jan 8, 2006, in forum: C Programming
    Replies:
    5
    Views:
    365
Loading...

Share This Page