Person(string myName, int myAge)
{
setName( myName);
setAge(myAge);
}
};
int main(int Person[])
{
Person people[100];
int count=0;
string name="";
int age =0;
cout<<"Enter name (-1 to stop): ";
cin >>name;
people[count].setName(name);
cout<<"Enter age of "<< name <<" :";
cin >> age;
people[count].setAge(age);
while(people.getName()=="-1")
{
count=count+1;
cout<<"Enter name (-1 to stop): ";
cin >>name;
people[count].setName(name);
cout<<"Enter age of "<< name <<" :";
cin >> age;
people[count].setAge(age);
}
for(int i=0; i<count; i++)
{
cout <<" Name: " << people[i].getName()<< ", age: "<< people[i].getAge()<< endl;
}
int min = people[0].getAge();
string minName = people[0].getName();
for(int i=0; i<count; i++)
{
for(int j=1; j<count; j++)
{
if (people[i].getAge() > people[j].getAge())
{
min = people[j].getAge();
people[j].setAge(people[i].getAge());
people[i].setAge(min);
minName = people[j].getName();
people[j].setName(people[i].getName());
people[i].setName(minName);
}
}
You need to define a default constructor. Add in the public section of the class: Person() : age(0) {}
Also, if you are going to call getName() in the condition of the while loop you would need : while(people[count].getName()!="-1")
and not
while(people.getName()!="-1").
There are a few other subtle problems.
Inside the while loop on the line cin >>name; if the user enters "-1" your code still continues and stores the name "-1" and prompts for the age of the person named "-1".
What happens if the user enters more than 100 persons?