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.i need this type of output

Please enter the number of siblings:   4
Please enter the particulars of each sibling:
Please enter the particulars of Sibling: 1
Name:     Arif    Age:   20
Please enter the particulars of Sibling: 2
Name:     Sana   Age:   16
Please enter the particulars of Sibling: 3
Name:     Sobia  Age:   13
Please enter the particulars of Sibling: 4
Name:     Amna    Age:   10

Arif, having age 20 is the eldest sibling whereas Amna, having age 10 is the youngest
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 Siblingperator >( 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

Quick diagnosis for : the thread's code shows several common C++ mistakes — outdated headers like <iostream.h>, manual char* allocation errors (e.g. new char(len+1) instead of new char[len+1]), reading into an unallocated char*, wrong loop bounds/variable shadowing, a setage check that inspects the wrong variable, a malformed header-guard, and an attempted sort implementation that doesn't make sense as declared. was right to flag <iostream.h> and indentation: modern, safe code uses std::string, std::vector, and the algorithms in <algorithm> so you avoid all of those pitfalls.

A concise, robust approach: store names in std::string, collect siblings in a std::vector, then use std::min_element / std::max_element (or std::sort) by age. The code below is a minimal, modern example that produces the requested output and sidesteps manual memory management:

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

struct Sibling {
    std::string name;
    int age = 0;
    Sibling(std::string n = "", int a = 0) : name(std::move(n)), age(a) {}
};

int main() {
    int n;
    std::cout << "Please enter the number of siblings: ";
    if (!(std::cin >> n) || n <= 0) return 0;
    std::vector<Sibling> v; v.reserve(n);
    for (int i = 0; i < n; ++i) {
        std::string nm; int a;
        std::cout << "Name: "; std::cin >> nm;
        std::cout << "Age: "; std::cin >> a;
        v.emplace_back(nm, a);
    }
    auto cmp = [](const Sibling& x, const Sibling& y){ return x.age < y.age; };
    auto minit = std::min_element(v.begin(), v.end(), cmp);
    auto maxit = std::max_element(v.begin(), v.end(), cmp);
    std::cout << maxit->name << ", having age " << maxit->age
              << " is the eldest sibling whereas "
              << minit->name << ", having age " << minit->age
              << " is the youngest\n";
}

Notes and troubleshooting tips: use zero-based loops (for (int i = 0; i < n; ++i)), validate input (if (!(std::cin >> x))), and prefer std::getline when names can contain spaces. If learning manual memory management is required, fix allocations to new char[strlen(...) + 1], ensure the copy constructor allocates and copies safely, and match delete[] in the destructor. Avoid conio.h/conio.c for portable code. If an operator is needed, implement bool operator>(const Sibling& other) const { return age > other.age; }, but for most sorting tasks a comparator is clearer and safer.

You shouldn't use <iostream.h>, and you should use indentation.

Sibling Siblingperator >( int *array, int arraysize)

I think you're missing a few characters there.

}
>()
return 0;
}

Eh? What's that?

[edit]
Cross-post: http://daniweb.com/techtalkforums/showthread.php?p=178088
[/edit]

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.