send to function
Jan 30, 2011 at 11:46am UTC
Hi there!
I have a function in club.cpp that looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
void Club::addMember(string name, string birthDate)
{
if (this ->nrOfMembers==this ->capacity)
{
this ->capacity+=2;
Member **temp=new Member*[this ->capacity];
for (int i=0;i<this ->capacity;i++)
{
temp[i]=NULL;
}
for (int i=0;i<this ->nrOfMembers;i++)
{
temp[i]=this ->members[i];
}
delete [] this ->members;
this ->members=temp;
temp=NULL;
}
this ->members[this ->nrOfMembers++]= new Member ( name, birthDate);
}
and from a function in main I want to send strings to the function above, and our teacher showed us how it was supposed to be done, but looks like I've forgotten a small detail. This is what I've written so far.
1 2 3 4 5 6 7 8 9 10
void addMember(string name, string birthDate)
{
cout<<"Name: " ;
cin.ignore();
getline(cin, name);
cout<<"Born: " ;
getline(cin, birthDate);
Club c1.addMember(name, birthDate);
}
I know that the function overload is not the best but I'll fix that later.
My question is how should the last line look? I would like to send name and birthDate to the first function that i wrote above.
thanks in advance.
Jan 30, 2011 at 12:16pm UTC
First, the current set-up doesn't make sense.
What you may want to do, but you have to decide from your problem description:
1. Have a function that prompts the user for name and birth-date and inserts new record into the collection:
1 2 3 4 5 6 7 8 9 10
void addMember(Club& c)
{
cout<<"Name: " ;
cin.ignore();
getline(cin, name);
cout<<"Born: " ;
getline(cin, birthDate);
c.addMember(name, birthDate);
}
And you can use it from the top-level code:
1 2 3 4 5 6 7 8
int main()
{
//...
Club c; //Assuming there is default constructor
//...
addMember(c);
//...
}
2. Create a
Club object in the top-level code and do the entire procedure directly there:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
//...
Club c; //Assuming there is default constructor
//...
cout<<"Name: " ;
cin.ignore();
getline(cin, name);
cout<<"Born: " ;
getline(cin, birthDate);
c.addMember(name, birthDate);
//...
}
Regards
Jan 30, 2011 at 12:25pm UTC
Thanks a lot dude, the thing that i forgot is the call-by-reference.
Everything works like a charm now.
Topic archived. No new replies allowed.