I am try to write a C++ program that tells the eldest and youngest sibling in a family. i try to write write a member function that overloads the > Operator to sort the Siblings according to their ages after making comparisons.but i am fail. please someone write this. thanks
my program files are given below:
Sibling.cpp

#include <iostream.h>
#include <string.h>
#include <conio.h>

#include "Sibling.h"

Sibling::Sibling(const Sibling &obj)
{
int len=strlen(obj.name);
name=new char(len+1);
strcpy(name,obj.name);
age=obj.age;
}

void Sibling::setname(char *SiblingName)
{
name= new char(strlen(SiblingName+1));
strcpy(name,SiblingName);
}
void Sibling::setage(int SiblingAge)
{
if (age<=0)
{
cout<<"negative age is not allowed";
age=0;
}
age=SiblingAge;
}
char *Sibling ::getname()
{
return name;
}
int Sibling:: getage()
{
return age;
}

mail.cpp

#include <iostream.h>
#include <conio.c>
#include <string.h>
#include "Sibling.h"

int main()
{
int a=0 ;
char *nameofsibling=NULL;
int ageofsibling=0;
cout<<"Plz enter the no. of Sibling";
cin>>a;
Sibling siblingobj[a];

for(a=1;a<=a;a++)
{
cout<<"plz enter the particular of Sibling"<<a<<endl;
cout<<"name:";
cin>>nameofsibling;
cout<<"age:";
cin>>ageofsibling;
siblingobj[a].setname(nameofsibling);
siblingobj[a].setage(ageofsibling);
}
>()
return 0;
}

Sibling.h

#ifndef "Sibling.h"
class Sibling{
private:
         char *name;
         int age;
public:
       Sibling(); //constructor
       Sibling(const Sibling &obj);//copy constructor
       void setname(char *);//setter functions
       void setage(int a);
       char *getname();    //getter functions
       int getage();
       Sibling Sibling:operator >( int *array, int arraysize)
{
  for (int pass=0; pass<arraysize;pass++)
     for(int i=0;i<arraysize;i++)
     if a[i]>a[i+1];
     {
     int *temp=a[i];
     a[i]=a[i+1];
     a[i+1]=*temp;
     }
     
}
      
       
       ~Sibling()//destructor
{
if (name)
         {
         delete[]name;
          }
}
       };

Dani AI

Generated

The code posted by tries to do the right thing but has multiple bugs and unsafe patterns (manual char* management, wrong allocations, bad loop indices, non-standard variable-length arrays, broken copy/setter logic). was right to point at defining a comparison and using std::sort — that is a clean option — and / are correct that the discussion should stay in one thread. The simplest fix is to stop managing raw C strings and arrays and use standard containers: std::string for names and std::vector for the collection. That removes the need for a manual destructor and avoids most of the memory mistakes in the original code.

Common concrete fixes to apply:

  • Use <iostream>, <string>, <vector>, <algorithm> and avoid obsolete headers like <iostream.h> or <conio.h>.
  • Do not allocate with new char(len+1) — the correct form (if using char*) is new char[len+1]. Better: use std::string and avoid manual allocation entirely.
  • Read the number of siblings, validate it, then reserve a std::vector<Sibling>.
  • Implement a member comparison (operator<) or supply a comparator to std::sort; std::sort expects a strict weak ordering (age only is fine here).
  • Index arrays from 0 to n-1, not 1..n.
  • If only oldest and youngest are needed, a single-pass min/max keeps O(n) complexity and avoids sorting.

Example (safe, standard approach):

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits>

class Sibling {
    std::string name;
    int age;
public:
    Sibling(const std::string& n = "", int a = 0) : name(n), age(a) {}
    const std::string& getName() const { return name; }
    int getAge() const { return age; }
    bool operator<(const Sibling& o) const { return age < o.age; }
};

int main() {
    int n;
    std::cout << "Number of siblings: ";
    if (!(std::cin >> n) || n <= 0) return 1;
    std::vector<Sibling> v; v.reserve(n);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    for (int i = 0; i < n; ++i) {
        std::string name; int age;
        std::cout << "Name: "; std::getline(std::cin, name);
        std::cout << "Age: ";
        while (!(std::cin >> age) || age < 0) {
            std::cout << "Invalid age. Try again: ";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        v.emplace_back(name, age);
    }
    std::sort(v.begin(), v.end());
    if (!v.empty()) {
        std::cout << "Youngest: " << v.front().getName() << " (" << v.front().getAge() << ")\n";
        std::cout << "Oldest:   " << v.back().getName()  << " (" << v.back().getAge()  << ")\n";
    }
}

Troubleshooting tips: validate inputs, check vector empty before front()/back(), and prefer a single linear scan with two variables (min/max) if performance or memory matters. This approach removes the risky pointer math and makes the behavior predictable.

Recommended Answers

All 3 Replies

bool Siblinb::operator<( const Sibling &other )
{
    return age < other.age ;
}

...
#include <algorithm>
... 

  std::sort( siblingobj, siblingobj + num_siblings ) ;
...

Let's use the other thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.