okay so here's my code but i knwo there's just something so wrong with the second/youngest one. the first/oldest part works though.

    int oldest = 0;
    // Determine and print oldest employee(s):
    for (int i=0; i<recordlist.size(); i++) {
        if (recordlist[i].age > oldest){
            oldest = recordlist[i].age;
        }
    }
    cout << "Oldest employee(s): "
    << endl;
    for (int o=0; o<recordlist.size(); o++) {
        if (recordlist[o].age == oldest) {
            cout << "\t" << recordlist[o].name << "; " << recordlist[o].age << endl;
        }
    }
    cout << endl;

    // Determine and print youngest employee(s):
    int youngest=0, y=0; int u=0; //****************************************
    for (int u=recordlist.size(); recordlist[u].age>recordlist[u-1].age; u--) {
        youngest=recordlist[u].age;
        if (recordlist[u-1].age < youngest){
            youngest = recordlist[u-1].age;
        }
    }
    cout << "Youngest employee(s): "
    //<< recordlist[o].name << "; " << recordlist[o].age
    << endl;
    for (int y=0; y<recordlist.size(); y++) {
        if (recordlist[y].age == youngest) {
            cout << "\t" << recordlist[y].name << "; " << recordlist[y].age << endl;
        }
    }
    cout << endl;

Line 19 - 23 : Why can't you use a similar loop condition as used in line 3 - 7

Edited 4 Years Ago by subith86

The reason you are having an issue is you are setting youngest equal to 0 before you start the for loop and as long as you have valid data you shouldnt have an employee with a zero age.

// change
youngest = 0;
// to 
youngest = recordlist[0].age

The condition is your for loop looks weird as well as the if statement fot the youngest part. You should use the exact same loop and if statement that you used to find the oldest for finding the youngest. The only difference is the you need to change > to < in the if statement and change oldest to youngest

 int youngest = recordlist[0].age;    // Determine and print youngest employee(s):  
 for (int i=0; i<recordlist.size(); i++) 
 {
     if (recordlist[i].age < youngest)
         youngest = ecordlist[i].age
 }
This article has been dead for over six months. Start a new discussion instead.