Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • C++ Problem

    • 1
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 649
    Answer it


    To input the records of students. Display the whole record. Display the name of the youngest and eldest student.

    I'm having problem in achieving the third target: If I enter 3 or 5 entries the result is fine but as soon as I enter 5 or 10 entries, the eldest student name shows up but the youngest name does not.


 2 Answer(s)

  • After going through your code, i found that inside the "checkage()" function, you have initialized the variables "eldest" and "youngest" to "age[0]" (i.e whatever be your element at 0th index). And inside both the for loops you are checking the condition
    if(age[i ]> eldest) and if(age[i] < youngest),
    these conditions will evaluate to false if your "youngest" or "eldest" person's age is present at 0th index(age[0]) (Because (a < a) or (a > a) is always false, i.e no integer number can be smaller or greater than itself).Thus "e_name" & "y_name" will never get initialized in such case. And u won't be able to print there values in this condition.

    To overcome this, either you can initialize "e_name" & "y_name" to name[0] initially. like:
    e_name =name[0];
    y_name = name[0];

    Or, instead of "<" and ">" you can check for "<=" and ">=" inside the if conditions.

  • Hello there, There was one problem in your if condition inside "checkage()" function. You assigned youngest and eldest to a[0] in this case if eldest or oldest be at index 0 then your if condition will return false ( 1 < 1 will return false ) please uncomment your program accordingly as i was testing in linux environment, so i had to comment some lines..

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: