#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
class Student{
private:
int size;
public:
Student object (int s = 1) {size = s;}
char Firstname[20];
char Lastname[20];
int grades[4];
double average;
void calgradeaverage(void);
};
void Student::calgradeaverage(void)
{
average = 0;
shortunsignedint index;
for(index=0; index<4; index++){
average+=grades[index];
}
average /= 4;
cout<<average<<endl;
};
int main(void)
{
Student object[29];
for (int i = 0; i < 29; 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();
}
;
}
I am following the tutorial in the book and in the progress of experimenting with pointers and its difficult. If someone can help me out or point me into the right direct i would be appreciated. The best way to explain to me is first, tell me why i am not doing it right, then tell me how i can get around it.
i tried
1 2 3 4 5
//in main
int s;
cout<<"enter the size of array of the object";
cin>>s;
Student object[s]; //<--- doesnt let me do that
1)as you can see, i hard code to make the object array to be 29, can anyone teach me how to make it possible for the user to input the size of array?
2)Then dynamically allocate this array of structures.
3)afterwards have all members of all structure variables be assigned values via the pointer used in dynamically allocate the memory
I dislike dynamic arrays, but here's how you do it.
1 2 3 4 5 6 7 8 9 10
int s = 0;
//get s from the user here, I'd read in a single character and convert it to an int,
//just to be sure they don't enter something strange.. users tend to do that.
Object *instances = new (Object[s]); // this makes a new pointer to Object, then allocates some space
//IMPORTANT: to prevent memory leaks, please delete your newly allocated array of objects
delete[] instances;
The reason I dislike this is because you can use a vector, deque, or other STL container to dynamically change the size of the array, it's much more flexible and useful!!
#include <vector>
usingnamespace std;
int main(){
vector<int> intVec; //this makes a new vector that we will fill with ints
intVec.push_back(3);
intVec.push_back(5);
int temp = 8;
intVec.push_back(temp);
//intVec now holds 3,5,8
intVec.erase(intVec.begin()+1);
//intVec now holds 3,8
int temp2 = intVec[0] + intVec[1];
//temp2 now holds 11
return 0;
}
When you define an array like that you are telling the compiler "put this on the stack". However, in order to put it on the stack the compiler must know the size at compile time, which the value of s is only known at run time.
{
int s = 0;
int *instances;
Student object;
cout<<"enter the size";
cin>>s;
object *instances = new (object[s]);
for (int i = 0; i < 29; 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();
}
;
i don't really get what u mean it seems wrong. Do u need to creat the int* instance IN the class as a public pointer? If so isn't it suppose to be object.instance = new object[s];?