Reputation: 115
I am trying to write a c++ program where i need to accept name, age and address of 5 persons and display the name of the eldest person. I am able to find the age of the eldest person but confused on how to print the name corresponding to that age. Please have a look at my code and help me how to do that.
My C++ code is:
#include <iostream>
using namespace std;
struct student{
char name[20];
int age;
char add[40];
};
int main()
{
student stu[5];
int i, max;
for (i = 0; i <=4; i++)
{
cout << "\n Enter name";
cin >>stu[i].name;
cout << "\n Enter age";
cin >>stu[i].age;
cout << "\n Enter address";
cin >>stu[i].add;
}
max = stu[0].age;
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
max = stu[i].age;
}
cout << "Max age is: " <<max;
return 0;
}
I am able to find the maximum age. Please help me how to display the name of the person whose age is maximum
Upvotes: 1
Views: 17034
Reputation: 1
This is the edited version of your program, hope you like it. It shows all the details of the eldest person
//finding the highest age out of all the details of the student
#include < iostream >
using namespace std;
struct student
{
char name[20];
int age;
char add[40];
};
int main()
{
student stu[5];
int i, n, max;
cout << "\nenter the no. of record you want to enter : ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "\n Enter name : ";
cin >> stu[i].name;
cout << "\n Enter age : ";
cin >> stu[i].age;
cout << "\n Enter roll : ";
cin >> stu[i].add;
}
max = stu[0].age;
for (i = 0; i < n; i++)
{
if (stu[i].age > max)
max = stu[i].age;
}
cout << "\nHighest age report detail -----";
for (i = 0; i < n; i++)
{
if (stu[i].age == max)
{
cout << "\n your name : " << stu[i].name;
cout << "\n your age : " << stu[i].age;
cout << "\n your roll : " << stu[i].add;
}
}
return 0;
}
This program is totally compiled
Upvotes: -1
Reputation: 27538
Your code generally suffers from the lack of std::vector
and std::string
. You should not use arrays unless you have a good reason to use them.
Your struct student
should therefore be:
struct student{
std::string name;
int age;
std::string add;
};
Likewise, the array in main
should become:
std::vector<student> stu;
Notice how you no longer need to consider sizes.
The next thing is finding the eldest person. C++ already has facilities to find the greatest thing in something. In the <algorithm>
header, there is a function called std::max_element
which does exactly that. It works on anything which has a specified meaning for "less than". Two int
variables, for example, have a "less-than" relationship, such as in "4 is less than 5".
Your student
struct does not have such a natural relationship, but you can add it by providing operator<
:
struct student{
std::string name;
int age;
std::string add;
bool operator<(student const &other) const
{
return age < other.age;
}
};
This will then work:
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end());
int max_age = eldest_student_iter->age;
Now one may argue that this adds too much functionality into the student
struct itself, and it gives age
a greater importance than the other two members, which may not be logically justified.
Fortunately, std::max_element
has a second version which accepts a third parameter; a so-called functor which tells the function how to compare two elements. Here is an example:
struct StudentComparison
{
bool operator()(student const &lhs, student const &rhs) const
{
return lhs.age < rhs.age;
}
};
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
This should be nicely wrapped in a function of its own:
int GetHighestAge(std::vector<student> const &stu)
{
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
return max_age;
}
In real code, you'd also have to make up your mind on what should happen when there is no maximum element because the vector of students is empty.
int GetHighestAge(std::vector<student> const &stu)
{
if (stu.empty())
{
// error...
}
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
return max_age;
}
If you use C++11, the code can be written much more concisely. You no longer need to define the StudentComparison
functor elsewhere but can use a so-called lambda to define the comparison directly at the call site. And you can use auto
to spare you from the overly verbose std::vector<student>::const_iterator
.
auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });
Finally, here is a complete toy example:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct student{
std::string name;
int age;
std::string add;
};
int GetHighestAge(std::vector<student> const &stu)
{
if (stu.empty())
{
return -1;
}
auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });
int max_age = eldest_student_iter->age;
return max_age;
}
int main()
{
std::vector<student> stu;
// some test data:
student s1;
s1.name = "One";
s1.age = 18;
s1.add = "Somewhere";
student s2;
s2.name = "Two";
s2.age = 21;
s2.add = "Somewhere Else";
student s3;
s3.name = "Three";
s3.age = 20;
s3.add = "Yet Somewhere Else";
stu.push_back(s1);
stu.push_back(s2);
stu.push_back(s3);
std::cout << "Max age: " << GetHighestAge(stu) << "\n";
}
One final hint: Store the birth date rather than the age. Birth dates stay the same forever, ages become outdated.
Upvotes: 0
Reputation:
You can retain the index where the maximum was reached, this way you have access to all the information from that entry.
int maxi = 0;
for ( i = 1; i <=4; i++)
{
if (stu[i].age > stu[maxi].age)
{
maxi = i;
}
}
cout << "max age is " << stu[maxi].age << " and name is " << stu[maxi].name;
Upvotes: 2
Reputation: 217980
You may use std::max_element
from <algorithm>
which retrieves the iterator which points to the max element:
auto it = std::max_element(std::begin(stu), std::end(stu),
[](const student& lhs, const student& rhs) {
return lhs.age < rhs.age;
});
std::cout << "Oldest is " << it->name << " with " << it->age << std::endl;
Upvotes: 3
Reputation: 496
Just change type of "max" from int to Student-
student max;
max.age = stu[0].age;
strcpy( stu[0].name, max.name );
strcpy( stu[0].add, max.add );
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
max.age = stu[i].age;
strcpy( stu[i].name, max.name );
strcpy( stu[i].add, max.add );
}
Upvotes: 1
Reputation: 5056
You're almost there. Just keep the eldest when you find the age:
student eldest;
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
{
max = stu[i].age;
eldest = stu[i];
}
}
cout << "Max age is: " <<max << " named " << eldest.name;
return 0;
}
Upvotes: 2
Reputation: 836
Add another variable maxStudent of type student, which you set whenever your if statement is true. Then in the end you should have the correct student, for which you can print out every field.
Upvotes: 1
Reputation: 6224
Instead of storing the maximum age encountered, try storing a pointer to the record with the maximum age. Something like this:
student* max = &stu[0];
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max->age)
max = &stu[i];
}
std::cout << "Student with max age is " << max->name << std::endl;
Upvotes: 3