This is what I have so far, and it does give me the desired output, but I couldn't figure out how to use a dynamically allocated array. I wanted to figure out the problem without that first, so I can figure out how the rest of the code would work.
Instead of using a constant variable for the size of the array, how can I have the user input a desired amount that would then be used for the array?
class Person
{
private:
string name;
int age;
public:
// Default constructor
Person()
{
name = " ";
age = 0;
}
Person(string name1, int age1)
{
name = name1;
age = age1;
}
int getAge() { return age; }
string getName() { return name; }
// Mutator functions to set name and age
void setName(string name1);
void setAge(int age1);
};
int lengthOfName(Person *p);
// Main Function
int main()
{
string name;
int age;
const int SIZE = 3;
Person personArray[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the name:" << endl;
cin >> name;
personArray[i].setName(name);
cout << "Enter the age:" << endl;
cin >> age;
personArray[i].setAge(age);
}
for (int i = 0; i < SIZE; i++)
{
cout << "The name " << personArray[i].getName();
cout << " has length: " << lengthOfName(&personArray[i]) << endl;
}
}
// Returns the number of characters in a person's name *
int lengthOfName(Person *p)
{
string name = p->getName();
return name.length();
}
// Mutator function that sets variable name
void Person::setName(string name1)
{
name = name1;
}
// Mutator function that sets variable age
void Person::setAge(int age1)
{
age = age1;
}