Hi everyone.
I've been trying to write a code that do the following
The user write a number of words
The program store every word in memory
Here's the code i wrote :
bool more=true;
int d;
char ch1[40],ch2[40],ch3[1];
cout<<"Enter a word in capitals : \n";
cin>>ch1;
Darray T(ch1);
while(more==true)
{
cout<<"More words ? (y/n)"<<"\n";
cin>>ch3;
if(ch3[0]=='y')
{
cout<<"Enter a word in capitals : \n";
scanf("%s",&ch2);
T.Add(ch2);
cout<<"*********************************************************** \n";
T.print();
}
class Darray
{
private:
char word[20];
char** T;
int c;
public:
Darray(char ch[20]) //Constructor of the class.
{
this->c=1;
this->T=newchar*[1];
T[0]=ch;
}
void Add(char ch[20]) //Function to add elements to the array
{
char** P;
P=newchar*[c+1];
for(int i=0;i<c;i++)
{
P[i]=T[i];
}
P[c]=ch;
free(T);
T=P;
c++;
}
void print() //Print the element of the array to the screen
{
for(int i=0;i<c;i++)
{
cout<<T[i]<<"\n";
}
}
};
The problem is when i run it: it goes
Word1
Word2
The first time and :
Word1
Word3
Word3
The second time.
It's like if the second word was replaced by the third.
Thanks everybody.