i need to print the names as they appear in the original file, print the info of the person with the highest distance, print the info sorted by ascending ID number, and print sorted by name alphabetically. the first two parts work fine but sorting by ID and Name dont work. i was wondering if anyone could help?
int main()
{
char temp[50];
int count = 0;
ifstream In;
In.open("a1.txt");
if (In.good())
{
cout << "* * * a1.txt is FOUND! * * *" << endl;
cout <<"==============================" << endl;
}
else
{
cout << "* * * a1.txt is NOT FOUND! * * *" << endl;
cout << "Restart this program and make sure you place a1.txt\nin the same directory this source file is in" << endl;
cout <<"=======================================================" << endl;
system("PAUSE > null");
return 0;
}
while ( (In.peek()) != EOF )
{
In.getline(person[count].name, 30);
In.getline(temp, 50);
person[count].ID = atoi(temp);
In.getline(temp, 50);
person[count].distance = atol(temp);
In.getline(temp, 50);
cout << person[count].name << endl << person[count].ID << endl << person[count].distance << endl <<temp <<endl;
count++;
}
maxdis(9,person);
IDsort(9,person);
abcsort(9,person);
return 0;
}
void maxdis(int num, Node *person)
{
int max = 0;
for (int i=0; i<num-1; i++)
{
if (person[i].distance<person[i+1].distance)
max=i+1;
}
cout<<"The person with the max distance is: \n\n"<<person[max].name<<endl<<person[max].ID<<endl<<person[max].distance<<endl;
return;
}
void IDsort(int num, Node *person)
{
int count=0;
cout<<"\nSorted by ID:"<<endl;
for (int i=0; i < num-1; i++)
{
for (int j=0; j<num-1-i; j++)
{
if (person[j].ID>person[j+1].ID)
{
int count=person[j].ID;
person[j].ID=person[j+1].ID;
person[j+1].ID=count;
}
}
cout<<"\n"<<person[count].name<<endl<<person[count].ID<<endl<<person[count].distance<<endl;
count++;
}
return;
}
void abcsort(int num, Node *person)
{
cout<<"\nSorted by name:"<<endl;
char temp[50];
for (int i=0; i<num; i++)
{
if (strcmp(person[i].name,person[i+1].name)>0)
{
strcpy(temp,person[i].name);
strcpy(person[i].name,person[i+1].name);
strcpy(person[i+1].name,temp);
}
}
for (int i=0;i<num;i++)
cout<<"\n"<<person[i].name<<endl<<person[i].ID<<endl<<person[i].distance<<endl;
return;
}
for (int i=0; i < num-1; i++)
{
for (int j=0; j<num-1-i; j++)
{
if (person[j].ID>person[j+1].ID)
{
int count=person[j].ID;
person[j].ID=person[j+1].ID;
person[j+1].ID=count;
}
}
Should be:
1 2 3 4 5 6 7 8 9 10 11 12 13
for (int i=0; i < num; i++)
{
for (int j=i; j<num; j++)
{
if (person[i].ID<person[j+1].ID)//Move all Smaller ID's to the back
{
int count=person[i].ID;
person[i].ID=person[j+1].ID;
person[j+1].ID=count;
}
}
cout<<"\n"<<person[i].name<<endl<<person[i].ID<<endl<<person[i].distance<<endl;
}
Also because you have been actually moving around the variables in the array, you have actually corrupted your code because as you can see from the output in abcsort(), some names have the wrong distance or ID associated with them. So you have to come up with a better way of solving this then actually moving the contents of each array. Maybe use a copy of the input from the file to access the functions