This add function will take a single parameter which will be the item to be added to the array. I’ve tried using a for loop but it didn’t work as I expected it to. I’m currently just trying to do something like this:
bool homeworklist::add (homework h)
{
int i = 0;
if(current_size < LIST_MAX){
current_size += 1;
list[i] = h;
++i;
return true;
}
return false;
}
current_size is just a counter.
list is a array that belongs to a homeworklist class
list takes in homework objects.
The function returns true if the object was added succesfully and false if it wasnt added correctly.
The variable
iis local, which means each time you call this function, it is initialized to 0, due to this line:This in turn means in the subsequent
ifstatement, you are assigning the element to the first location, everytime.You could make
ia class member, just like how you might have declaredcurrent_size. Or might be you could just make use ofcurrent_sizeitself.