-
August 6th, 2012, 05:05 AM
#1
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.
-
August 6th, 2012, 06:19 AM
#2
Re: Output Sorting Error Using Pointer Notation[Beginners Question]
person::getsalary() doesn't return anything ( it should if I look at the declaration ) and it seems to be implemented as pure input function.
Kurt
-
August 6th, 2012, 07:45 AM
#3
Re: Output Sorting Error Using Pointer Notation[Beginners Question]
Originally Posted by ZuK
person::getsalary() doesn't return anything ( it should if I look at the declaration ) and it seems to be implemented as pure input function.
Kurt
Thanks for the suggestion, I actually didn't returned float value.
I modified my class Person
Modified Code
Code:
class person
{
protected :
string name;
float salary;
public :
void setdata()
{cout <<"\nEnter Name : "; cin >> name;
cout <<"\nEnter Salary : "; cin >> salary;}
void printdata()
{
cout <<"\nName : " << name;
cout <<"\nSalary : " << salary;
}
float getsalary()
{return salary;}
string getname()
{return name;}
};
But, Still, The pointer of object of person are not sorted according to their salary. I found somehow changes in output than before but still can't fullfill the expectation.
Awaiting Suggestion..
Regards
Basanta
-
August 6th, 2012, 08:30 AM
#4
Re: Output Sorting Error Using Pointer Notation[Beginners Question]
This implementation of bubble sort looks wrong:
Code:
for(i=0;i<n-1;i++)
for(j=1;j<i;j++)
order(c1+i,c1+j);
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|