how to dynamically allocate

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
int main(void)
{
	int s = 0;
	int *instances;

	Student object;
	cout<<"please enter the object size";
		cin>>s;
 
 object *instances = new (object[s]);


for (int i = 0; i < s; i++ ){
	cout<<"enter the data for the " <<i+1<<" student"<<endl;
	cout<<"enter First name ";
	cin>>object[i].Firstname;
	cout<<"enter last name ";
	cin>>object[i].Lastname;
	cout<<"enter your first test scores ";
	cin>>object[i].grades[0];
	cout<<"enter your second test scores ";
	cin>>object[i].grades[1];
	cout<<"enter your second test scores ";
	cin>>object[i].grades[2];
	cout<<"enter your second test scores ";
	cin>>object[i].grades[3];
	cout<<" the average test score for " <<i+1<<"student is ";
	object[i].calgradeaverage();
	
}

;

delete[] instances;
	
	
}


i want the user to be able to input the size of the array
1) You have instances defined twice, only define it once
2) Remove the () around object[s]
3) http://cplusplus.com/forum/articles/40071/#msg218019
cannot,
1
2
3
4
5
6
7
8
9
	
	int s = 0;


	Student object;
	cout<<"please enter the object size";
		cin>>s;
 
 object *instances = new object[s]//instances and object is underline with red it said instances is undefinted; 


Have you defined this "object" class?

int *instances; //this is wrong cos instances will point to Student (which is class name) later soREMOVE THIS

Student object; // wht's this ?? you want this object to be dynamicaly alocated wright? REMOVE THIS TOO
1
2
3
	cout<<"please enter the object size"; //OK
		cin>>s; //OK
 

object *instances = new (object[s]); //FATAL ERROR this has no meaning you cna't create array from object !!...
create dynamic array of objects like this:
1
2
Student* instances = new Student [s]; //this will create s objects of class Student dynamicaly.
pointer will point to each object...

THIS IS ALL WRONG:
1
2
3
4
5
6
7
for (int i = 0; i < s; i++ ){ 
	cout<<"enter the data for the " <<i+1<<" student"<<endl;
	cout<<"enter First name ";
	cin>>object[i].Firstname;
	cout<<"enter last name ";
	cin>>object[i].Lastname;
        //ETC... 

u must do int like this
1
2
3
4
5
6
7
for (int i = 0; i < s; i++ ){  
	cout << "enter the data for the " <<i+1<<" student"<<endl;
	cout << "enter First name ";
	cin >> instances [i].Firstname;
	cout<<"enter last name ";
	cin>> instances [i].Lastname;
        //ETC... 

now each instance will have it's own student object with it's own initializaiton.
there is one more error:
u didn't include class header or you didn't provide it here
did u implement class Student???
you have forgot return 0; too !!
Last edited on
thanks alot...
Topic archived. No new replies allowed.