
Originally Posted by
lionheart
Thanks, people...
Now it is working and I don't understand why (the idea of constant vs. cin... it's stupid)...
here's the working version...
Code:
void main(){
char *t, *s;
t=new char[200];
s=new char[100];
if (t==NULL || s==NULL)
exit(1);
else
{
cin>>t; //here's the correction
cin>>s; //here's the correction
strcat(t,s);
cout<<t;
}
delete []t;
delete []s;
}
What's the idea of constant vs cin? You can use string literals, you just have to COPY it into the char array using strncpy or in the constructor of the char array.
Code:
void main()
{
char t[200], s[100]; //why use dynamic allocation?
strncpy(t, "Hello World", sizeof(t)); //copy "Hello World" to t
strncpy(s, "avi", sizeof(s)); //Copy "avi" to s
strcat(t,s); //call that function you made
printf("%s\n", t); //display results
}
You may also copy string literals like this:
Code:
char t[] = "Hello World"
It only works in the constructor and the resulting size of the array will only be as long as the string + 1 so you couldnt copy anything else to it