Dinamic array

Hello everyone, i have problems with this: I want then the user enter a number of words, and they will be save in a dinamic array... here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
    char aux[20],*cad,*tem;
    int x=1;
    cad=new char [1];
    for (int a=0;a < 4;a++){
        cin>>aux;
        x=x+strlen(aux);
        tem=new char [x-strlen(aux)];
        strcpy(tem,cad);
        cad= new char [x];
        strcpy(cad,tem);
        strcpy(cad,aux);
    }
    cout<<cad<<endl;




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

PD: I don't know english so much... Sorry...

Bye, i hope your help.
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

1
2
3
4
5
6
7
8
9
10
11
12
13
    char aux[20],*cad,*tem;
    int x=1;
    cad=new char [1];
    for (int a=0;a < 4;a++){
        cin>>aux;
        x=x+strlen(aux);
        tem=new char [x-strlen(aux)];
        strcpy(tem,cad);
        cad= new char [x];
        strcpy(cad,tem);
        strcpy(cad,aux);
    }
    cout<<cad<<endl;


I know to use the string class, but i'm practicing :S although i can't solve the problem...
In the spirit of the original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    char aux[20],*cad,*tem;
    int x=1;
    cad=new char [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=new char [x-strlen(aux)];
        strcpy(tem,cad);

        //ERROR: prevous allocated memory  used by cad is not freed before re-allocation;
        cad= new char [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 
Last edited on
1
2
3
4
   
    cad=new char [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*/


I have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    char aux[20],*cad,*tem;
    int x=1;
    for (int a=0;a < 4;a++){
        cin.getline(aux,20);
        x=x+strlen(aux);
        tem=new char [x-strlen(aux)];
        strcpy(tem,cad);
        delete[] cad;
        cad= new char [x];
        strcpy(cad,tem);
        strcpy(cad,aux);
        delete[] tem;
    }
    cout<<cad<<endl;
    
    delete[] cad;

What i have to use for concatenate the string??
Because continue showing the last word... Thaks "guestgulkan" ...
Last edited on

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=new char [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=new char [x-strlen(aux)];
        strcpy(tem,cad);

        delete []cad; //deallocate
        cad= new char [x];
        strcpy(cad,tem);
        strcat(cad,aux);//strcat -  not strcpy
        delete [] tem;//de-allocate
    }
    cout<<cad<<endl;
    delete [] cad;//de-allocate  

Last edited on
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: The above was directed at the OP
Last edited on
Maybe like this you understand the code like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
    char aux[20],*cad,*tem;
    for (int a=0;a < 4;a++){
        cin.getline(aux,20);
        tem=new char [strlen(cad)];
        strcpy(tem,cad);
        delete[] cad;
        cad= new char [strlen(tem)+strlen(aux)];
        strcpy(cad,tem);
        strcpy(cad,aux);
        delete[] tem;
    }
    cout<<cad<<endl;
    


I'll read the link... and after edit this reply

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)...
Last edited on
If he had put the lines this way around - it would be more obvious:
1
2
       tem=new char [x]; //create a buffer  equal to current length
       x=x+strlen(aux); //calculate new length 

Yeah xD sorry... But what is wrong with the code? the call to strcpy()?
Has anyone else noticed that people here are going retrograde, using C strings instead of C++ strings?

This seems wrong to me, though it may be my caffeine dependency:
1
2
3
4
5
    char aux[20],*cad,*tem;
    for (int a=0;a < 4;a++){
        cin.getline(aux,20);
        tem=new char [strlen(cad)];
        strcpy(tem,cad);


How big is *cad? More importantly, what is in *cad?

guestgulkan had it right when he added a line:
cad=new char [1]();

-Albatross
Last edited on
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=new char [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=new char [x-strlen(aux)];
        strcpy(tem,cad);

        delete []cad; //deallocate
        cad= new char [x];
        strcpy(cad,tem);
        strcat(cad,aux);//strcat -  not strcpy <<=====
        delete [] tem;//de-allocate
    }
    cout<<cad<<endl;
    delete [] cad;//de-allocate   


Look up information about strcat and strcpy
Last edited on
Last edited on
cad=new char [1]();// initialise it to zero - that will make it an empty string

I don't knew it... What is happening there? call a constructor? or something like that...?

strcat(cad,aux);

Se me habia olvidado xD...

Thanks evaryone, now the code work but sometimes, do not... If you write:
Array
Dinamic
Out of the program...

And when you write:
What
are
you
doing

show:
h+;whatareyoudoint

Why the h+;??

Again, thaks everyone...

EDIT: i resolve the problem of "h+;" at first... And now work... I think then that 3 characters, make crazy the program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char aux[20],*cad,*tem;
    cad=new char [1]();
    strcpy(cad,"");
    for (int a=0;a < 4;a++){
        cin.getline(aux,20);
        tem=new char [strlen(cad)];
        strcpy(tem,cad);
        delete[] cad;
        cad= new char [strlen(tem)+strlen(aux)];
        strcpy(cad,tem);
        strcat(cad,aux);
        delete[] tem;
    }
    cout<<cad<<endl;
    
    delete[] cad;


cad=new char [1]();// initialise it to zero - that will make it an empty string

0o?? what happend?
Last edited on
Now i see a problem with the program... sometimes not work...
If you write:
Hello
how
are
... Here, out of the execution...

i don't know why...?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    char aux[20],*cad,*tem;
    cad=new char [1]();
    //strcpy(cad,"");//Not necessary
    for (int a=0;a < 4;a++){
        cin.getline(aux,20);
        tem=new char [strlen(cad)+1];//<< Need to add one  slot for the terminating 0
        strcpy(tem,cad);
        delete[] cad;
        cad= new char [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;



Last edited on
Any chance that you'll use C++ strings sometime, Nevo?

http://cplusplus.com/reference/string/
http://cplusplus.com/reference/string/getline/
http://cplusplus.com/reference/string/string/

Trust me. They're a lot easier to use.

-Albatross
+1 Albatross about using std::strings instead of char*
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...

Thanks everyone...
Topic archived. No new replies allowed.