CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Output Sorting Error Using Pointer Notation[Beginners Question]

    Greeting Again Codeguru World!

    My code compiled well(After long Messing up with my head).
    But, i still not satisfied of my output as i expected.
    My code ought to sort the object of person comparing their salary. But, its not.

    My code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class person
    {
        protected :
            string name;
            float salary;
        public :
            void setdata()
                {cout <<"\nEnter Name : "; cin >> name;}
            void printdata()
                {
                    cout <<"\nName : " << name;
                    cout <<"\nSalary : " << salary;
                }
            float getsalary()
            {cout <<"\nEnter Salary : "; cin >> salary;}
            string getname()
                {return name;}
    };
    
    int main()
    
    {
        void bsort(person**, int);
        person* persptr[100];
        int n=0;
        char choice;
        do
        {
            persptr[n] = new person;
            persptr[n]->setdata();
            persptr[n]->getsalary();
            n++;
            cout <<"\nEnter Another(y/n)? "; cin >> choice;
        }while(choice=='y');
        cout<<"\nUnsorted List\n";
            for(int i=0;i<n;i++)
                persptr[i]->printdata();
        bsort(persptr, n);
        cout<<"\nSorted List\n";
            for(int i=0;i<n;i++)
                persptr[i]->printdata();
        cout << endl;
        return 0;
    }
    
    void bsort(person** c1, int n)
    {
        void order(person**, person**);
        int i,j;
        for(i=0;i<n-1;i++)
            for(j=1;j<i;j++)
                order(c1+i,c1+j);
    }
    
    void order(person** p1, person** p2)
    {
        if((*p1)->getsalary()>(*p2)->getsalary())
        {
            person* tempptr = *p1;
            *p1 = *p2;
            *p2 = tempptr;
        }
    }
    It doesn't sort the object of class person rather than it prints out the stored value as it is.
    Thank you.

    Regards
    Basanta
    Last edited by basanta; August 6th, 2012 at 05:09 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured