hi all hope u r fine and good
i got a problem here with this code and i need little help
my problem that the program stops immediately with a close message
to be honest i forget also to release the memory in the program because i didn't remember at first time but after i did the releasing the problem still exists that's the code hope u can help me finding a solution and thanks :) :)
You need to do the allocation when you actually know the size of the array. After this line: cin >> pointer[i].numberOfChildren;
By the way:
1 2 3 4 5 6 7 8 9
information()
{
numberOfChildren=0;
string* childrenNames = new string[numberOfChildren]; // Note: this is a local variable. The member variable is not affected
length=0;
width=0;
height=0;
int* volume = newint[numberOfChildren]; // Note: this is a local variable. The member variable is not affected
}
first of all thanks for your help bro :) :)
u r completely right i did what u said but the varibles still unknown for it this the code after modification
#include <iostream>
#include <string>
usingnamespace std;
shortunsignedint scenario;
int _volume(int,int,int);
class information
{
public:
int numberOfChildren;
int length,width,height;
string* childrenNames;
int* volume;
information()
{
numberOfChildren=0;
childrenNames = new string[numberOfChildren];
volume = newint[numberOfChildren];
childrenNames = nullptr;
volume = nullptr;
length=0;
width=0;
height=0;
}
~information()
{
delete[] childrenNames;
delete[] volume;
}
};
void main()
{
cout << "enter the number of scenarios " << endl;
cin >> scenario;
information* pointer = new information[scenario];
for (int i=0; i< scenario ; i++ )
{
cout << " enter the number of children " << endl;
cin >> pointer[i].numberOfChildren;
string* childrenNames = new string[pointer[i].numberOfChildren];
//here and the next line compilor treats the childrenNames and volume as a new varibles like what was happening before inside the constructor
int* volume = newint[pointer[i].numberOfChildren];
for (int j = 0; j < pointer[i].numberOfChildren ; j++ )
{
cout << " please enter the name of the student " << endl;
cin >> pointer[i].information::childrenNames[j];
cout << " please enter the length,width and height " <<endl;
cin>> pointer[i].length;
cin>> pointer[i].width;
cin>> pointer[i].height;
pointer[i].volume[j] = _volume(pointer[i].length,pointer[i].width,pointer[i].height);
}
for(int g=0;g < pointer[i].numberOfChildren;g++)
{
if (pointer[i].volume[g]!=pointer[i].volume[g+1])
cout << pointer[i].childrenNames[g] <<" has lost Jelly to"<< pointer[i].childrenNames[g+1]<< endl;
else
cout << "no child has lost its jelly trick faild " << endl;
}
}
delete[] pointer;
}
int _volume(int x,int y,int z)
{
return x*y*z;
}