Hi, in this program I am adding adding person objects into person * persPtr[50];
ID: 3759046 • Letter: H
Question
Hi, in this program I am adding adding person objects into person * persPtr[50]; and then sorting it by the person's name or salary.
What I can't do is after I sort the first few names or salary and try to run it again the same names appear again.
I need help by clearing the names and salary to begin it again.
Quick overview: I need to clear the names and salary after I run the program the first time so that the second run can run with completely new names and salary.
(As you can see the names and salary appear twice when I run the program the second time)
Here is the coding of the main:
int main()
{
char choice;
person * persPtr[50];
int n = 0;
bool s = true;
while(true)
{
do {
persPtr[n] = new person;
persPtr[n]->setPerson();
n++;
cout <<"Enter a another person? [y/n]";
cin >> choice;
}while(choice == 'y');
cout<< "Unsorted list:" << endl;
for(int i = 0; i < n; i++)
{
persPtr[i]->printName();
persPtr[i]->printSalary();
}
bsort(persPtr, n, s);
cout<< "Sorted list:" << endl;
for(int i = 0; i < n; i++)
{
persPtr[i]->printName();
persPtr[i]->printSalary();
}
}
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
while(true)
{
char choice;
person * persPtr[50];
int n = 0;
bool s = true;
do {
persPtr[n] = new person;
persPtr[n]->setPerson();
n++;
cout <<"Enter a another person? [y/n]";
cin >> choice;
}while(choice == 'y');
cout<< "Unsorted list:" << endl;
for(int i = 0; i < n; i++)
{
persPtr[i]->printName();
persPtr[i]->printSalary();
}
bsort(persPtr, n, s);
cout<< "Sorted list:" << endl;
for(int i = 0; i < n; i++)
{
persPtr[i]->printName();
persPtr[i]->printSalary();
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.