strcat problem

C

ctara_shafa

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?
 
I

Ico

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
 
A

abdur_rab7

No it does not work as you think

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
 
A

abdur_rab7

No it does not work as you think

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
 
A

abdur_rab7

No it does not work as you think

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
 
M

M.B

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.
 
K

Keith Thompson

Ico said:
[..]
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

strcat problem 5
strcat and three strings 1
strcat and three strings 1
problem with strcat function 20
Taskcproblem calendar 4
Help with code plsss 0
Pascal - C (2) 57
Getting a bad input error, not sure why. 2

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top