I think the problem is in strcpy(tem,cad), maybe because, that funtion not copy each letter, just the direction, like a reference... Well, i don't really know what is the problem, so somebody help me pleace...
I acctually can't tell what you are doing from your code. Here is a link that can help with your code about half way down there is a loop simular to what you are trying to do. http://www.cplusplus.com/doc/tutorial/arrays/
"there is a loop simular to what you are trying to do" I don't see it... Anyway, thanks...
What i'm trying to do?... Well, i'll try to explain xD...
aux is for the word the user will enter
cad is all the string
tem is because (i think), i need save the cad characters before of give him memory with new... Because with the new operator, the cad content will be removed...
x is for to know the cad content... I don't use strlen... because i need give to tem the lenght of cad without the aux string... I know then is not the best way to do :S
char aux[20],*cad,*tem;
int x=1;
cad=newchar [1];//ERROR - this is an uninitialised array- the string length is undefined
for (int a=0;a < 4;a++)
{
cin>>aux;//WARNING: Chances of buffer overrun here
x=x+strlen(aux);
tem=newchar [x-strlen(aux)];
strcpy(tem,cad);
//ERROR: prevous allocated memory used by cad is not freed before re-allocation;
cad= newchar [x];
strcpy(cad,tem);
strcpy(cad,aux);//ERROR: This should not be the strcpy function
//ERROR: memory used by tem not freed before next re-allocation;
}
cout<<cad<<endl;
//ERROR: memory in use by cad not freed
cad=newchar [1];/* what you mean with "this is an uninitialised array".
So, in the first line what i did? 0o?
anyway, i see then that line have not sense (i don't know how to say it xD) is deleted :D*/
char aux[20],*cad,*tem;
int x=1;
cad=newchar [1]();// initialise it to zero - that will make it an empty string
for (int a=0;a < 4;a++)
{
cin.getline(aux,20); //
x=x+strlen(aux);
tem=newchar [x-strlen(aux)];
strcpy(tem,cad);
delete []cad; //deallocate
cad= newchar [x];
strcpy(cad,tem);
strcat(cad,aux);//strcat - not strcpy
delete [] tem;//de-allocate
}
cout<<cad<<endl;
delete [] cad;//de-allocate
Line 6 seems silly here. You add x to strlen(aux) on Line 5 and then on Line 6 you subtract the same value? Are you trying to use aux like a pointer? Line 7 might have the arguments reversed from what you are trying to accomplish see here: http://www.cplusplus.com/reference/clibrary/cstring/strcpy/. Again Line 7 and 8 you do something just to undo it on the next line, why? Can anyone explain this to me?
EDIT:
Well, strcpy(tem,cad); is what i want, to store the string cad for not lose her contain when i resize cad... When i resize, don't lose the string?? 0o? i think i do... And the other to calls to strcpy, are for concatenate what i have before in cad(then according to me is in tem), and what i got in aux (the new user word)...
Try this (this is what I posted a little while earlier) - then you can try it using c++ strings rather than arrays and see how much easier it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
char aux[20],*cad,*tem;
int x=1;
cad=newchar [1]();// initialise it to zero - that will make it an empty string
for (int a=0;a < 4;a++)
{
cin.getline(aux,20); //
x=x+strlen(aux);
tem=newchar [x-strlen(aux)];
strcpy(tem,cad);
delete []cad; //deallocate
cad= newchar [x];
strcpy(cad,tem);
strcat(cad,aux);//strcat - not strcpy <<=====
delete [] tem;//de-allocate
}
cout<<cad<<endl;
delete [] cad;//de-allocate
char aux[20],*cad,*tem;
cad=newchar [1]();
//strcpy(cad,"");//Not necessary
for (int a=0;a < 4;a++){
cin.getline(aux,20);
tem=newchar [strlen(cad)+1];//<< Need to add one slot for the terminating 0
strcpy(tem,cad);
delete[] cad;
cad= newchar [strlen(tem)+strlen(aux)+1];//<< Need to add one slot for the terminating 0
strcpy(cad,tem);
strcat(cad,aux);
delete[] tem;
}
cout<<cad<<endl;
delete[] cad;
Yeah i love the C++ strings jeje, first i do it with c++ strings, but was easy, thanks to "append" member, so i say: i have so much time (like 2 months) without use character's array, you know like C, and i wanna someday use C... And i was practicing... //strcpy(cad,"");//Not necessary
if i don't use it... the program show some rare characters at the beginning...