runtime error really need help here

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 :) :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#‎include‬ <iostream>
#include <string>
using namespace std;
short unsigned int scenario;
int _volume(int,int,int);
class information
{
public:
int numberOfChildren;
int length,width,height;
string* childrenNames;
int* volume;
information()
{
numberOfChildren=0;
string* childrenNames = new string[numberOfChildren];
length=0;
width=0;
height=0;
int* volume = new int[numberOfChildren];
}
~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;
for (int j = 0; j < pointer[i].numberOfChildren ; j++ )
{
cout << " please enter the name of the student " << endl;
getline(cin,pointer[i].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;
}
Last edited on
Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/

The problem is here:
1
2
3
4
5
6
information()
{
numberOfChildren=0;
string* childrenNames = new string[numberOfChildren]; // Note: write 'childrenNames = nullptr' instead
...
int* volume = new int[numberOfChildren]; // Note: write 'volume = nullptr' instead 
You allocate an array with the size 0.

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 = new int[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
using namespace std;
short unsigned int 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 = new int[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 = new int[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;
 }
 
Last edited on
=D it works o.O did that change
1
2
pointer[i].childrenNamenew=string[pointer[i].numberOfChildren];
pointer[i].volumenew=int[pointer[i].numberOfChildren];

thanks again bro i think i can complete the code now i appreciate ur help ^_^
Topic archived. No new replies allowed.