I have an "Element" class that has a "items" pointer, of type string (from the string.h library), responsible for saving a reference to an array of items in the Element. Everything works fine if I do not run the app within a while. However, while using the while, the program stops working if I try to print the items of the element I created earlier. To be clearer, what I did was exactly:
1 - Modify the element
2 - Show item items
When displaying element items, several strange characters appear on the console screen and the program stops working, without items even being printed on the screen.
Classes and application codes follow:
Class Element:
#include<iostream>
#include<string.h>
using namespace std;
class Elemento{
private:
string *itens;
int size;
public:
Elemento(string itens[], int size){
this->itens = itens;
this->size = size;
}
void showItens(){
for(int i = 0; i < this->size; i++){
cout<<endl<<itens[i]<<endl;
}
}
};
Application code:
#include<iostream>
#include "Elemento.cpp"
#include<string.h>
Elemento *el;
bool loop = true;
int main(){
while(loop){
int num;
cout<<endl<<"1 - Modificar o elemento"<<endl;
cout<<"2 - Mostrar os itens do elemento"<<endl<<endl;
cin>>num;
cin.sync();
switch(num){
case 1:{
int num;
cout<<"Digite o numero de itens: "<<endl;
cin>>num;
string itens[num];
for(int i = 0; i < num; i++){
cout<<"Digite o item: "<<i+1<<": "<<endl;
cin>>itens[i];
cin.sync();
}
el = new Elemento(itens,num);
el->showItens();
break;
}
case 2:{
if(el != NULL){
el->showItens();
}
break;
}
case 3:{
loop = false;
break;
}
}
}
}
Could anyone tell me the cause of this problem?
// Update
I have refactored the code using malloc. They looked like this:
Class Element:
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Elemento{
private:
string *itens;
int size;
public:
Elemento(string itens[], int size){
this->itens = (string*)malloc(size * sizeof(string));
this->itens = itens;
this->size = size;
}
void showItens(){
for(int i = 0; i < this->size; i++){
cout<<endl<<itens[i]<<endl;
}
}
};
App class:
#include<iostream>
#include "Elemento.cpp"
#include<string.h>
#include<stdlib.h>
Elemento *el;
bool loop = true;
int main(){
while(loop){
int num;
cout<<endl<<"1 - Modificar o elemento"<<endl;
cout<<"2 - Mostrar os itens do elemento"<<endl<<endl;
cin>>num;
cin.sync();
switch(num){
case 1:{
int num;
cout<<"Digite o numero de itens: "<<endl;
cin>>num;
string *itens;
itens = (string *)malloc(num * sizeof(string));
for(int i = 0; i < num; i++){
cout<<"Digite o item: "<<i+1<<": "<<endl;
cin>>itens[i];
cin.sync();
}
el = (Elemento*)malloc(sizeof(Elemento)) ;
el = new Elemento(itens,num);
el->showItens();
break;
}
case 2:{
if(el != NULL){
el->showItens();
}
break;
}
case 3:{
loop = false;
break;
}
}
}
}
I do not know if it is necessary to use malloc inside the constructor of the Element class. I believe the problem in my code is to iterate over the 'items' pointer. I saw in some sources that it was possible to iterate over it in the same way as an array, in the form 'items [index]'. However, my program still stops working in this part of the program.