Kirk Bevins wrote:
Thanx for the help!! I seem to be useless at c++ even tho I need to do
it!
My next problem is I cant respond to user input to create an amount of
student instances:
I have:
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];
for (int i=0; i<f; i++) {
Can't seem to compile! :(
Would appreciate any help pls.
I suppose your compiler is complaining about the fact the f is not
constant (needs to be when declaring student student[f]) and the
variable name should be different than the class name.
Hence, you need to allocate memory dynamically by use of new (don't
forget to use delete with [] later to ensure that the destructor of the
student class gets run). Anyhow, it seems to me that this is not where
you are in your course right now (been teaching this a bit myself a
while ago).
So, why not just declare an array of student of size 100 (since your max
input anyhow is 100) like:
Alternative 1 (no dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student theStudent[100];
if(f < 100)
{
for (int i=0; i<f; i++) {
theStudent[f].setSurname(...);
}// for
}// if
else
{
cout << "The input value is too large" << endl;
}
Alternative 2 (dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student* theStudent = student[f];
if(f < 100)
{
for (int i=0; i<f; i++) {
theStudent[f].setSurname(...);
}// for
}// if
else
{
cout << "The input value is too large" << endl;
}
// Make sure that the destructor of each student instance gets run.
delete [] theStudent;
/ Peter