C++ program to compare ages if 5 persons and print the name of the eldest person -
I am trying to write a C ++ program where I want to accept the names, ages and addresses of 5 people The greatest person I am capable of finding the age of the eldest person, but is confused on the way to print the name of that age. Please take a look at my code and help me how to do it.
My C ++ code is:
#include & lt; Iostream & gt; using namespace std; Structure Student {Core Name [20]; Old age; Four pairs [40]; }; Int main () {Student Stu [5]; Int i, Max; For {i = 0; i & lt; = 4; i ++} {cout & lt; & Lt; "Enter \ Nname"; Cin & gt; & Gt; Stu [i] .name; Cout & lt; & Lt; "Enter the age \"; Cin & gt; & Gt; Stu [i] .ge; Cout & lt; & Lt; "Enter the address \"; Cin & gt; & Gt; Stu [i] .add; } Max = stu [0] .ge; {If (stu [i] .age & gt; max) max = stu [i] .ge; for i (i = 0; i & lt; = 4; i ++) } Cout & lt; & Lt; "Maximum age is:" & lt; & Lt; Maximum; Return 0; }
I am able to find the maximum age. Please help me to display the name of the person whose age is the maximum
You can maintain the maximum, so you have access to all the information in that entry.
int maxi = 0; {If (stu [i] .age> stu [maxi] .age) for {i = 1; i & lt; = 4; i ++} {maxi = i; }} Cout & lt; & Lt; "Maximum age is" & lt; & Lt; Stu [Maxi]. Left & lt; & Lt; "And the name" is & lt; & Lt; Stu [Maxi] .name;
Comments
Post a Comment